当前位置: 首页 > 知识库问答 >
问题:

Springboot2和oauth

田玉韵
2023-03-14

我正在尝试使用 https://github.com/spring-projects/spring-security-oauth2-boot

使用本教程:https://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/

SpringBoot应用程序

@SpringBootApplication
@EnableAuthorizationServer
public class SafechatApplication {

    public static void main(String[] args) {
        SpringApplication.run(SafechatApplication.class, args);
    }

    @Bean
    public UserDetailsService a() {
        return new AuthServiceImpl();
    }

    @Bean
    public AuthenticationManager b() {
        return new OAuth2AuthenticationManager();
    }
}

服务器初始化器

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SafechatApplication.class);
    }
}

用户详细信息服务

@Service(value = "authService")
public class AuthServiceImpl implements UserDetailsService {

    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {

        return new org.springframework.security.core.userdetails.User("root", "admin", getAuthority());
    }

    private List<GrantedAuthority> getAuthority() {
        return Collections.singletonList(new SimpleGrantedAuthority("USER_ROLE"));
    }

    public List<UserDetails> findAll() {
        return Collections.singletonList(new org.springframework.security.core.userdetails.User("root", "admin", getAuthority()));
    }
}

尝试检索访问令牌时:

http://localhost:8080/oauth/token

叠:

2018-03-28 00:16:54.203 ERROR 6688 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'b' defined in cz.berger.safechat.SafechatApplication: Invocation of init method failed; nested exception is java.lang.IllegalStateException: TokenServices are required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at cz.berger.safechat.SafechatApplication.main(SafechatApplication.java:26) [main/:na]
Caused by: java.lang.IllegalStateException: TokenServices are required
    at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager.afterPropertiesSet(OAuth2AuthenticationManager.java:62) ~[spring-security-oauth2-2.2.1.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 16 common frames omitted

:bootRun FAILED

我试图解决问题,但现在它不会开始。我应该提供什么身份验证管理器?

共有1个答案

黄英韶
2023-03-14

您需要在config类中进行以下配置,并去掉应用程序类中的这两个bean

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired 
    @Qualifier("authService")
    private UserDetailsService userDetailsService;
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                 .userDetailsService(userDetailsService);
    }

    //....
}
 类似资料:
  • 我试着用Springboot2做一些测试,特别是用Reactor(Flux/Mono)。我面临一个问题。 当我尝试使用一个存储库方法做检索我所有的人我使用该方法:@Tailable Flux findRetTailableCursorBy(); 但就我而言,我收到了这个错误 当我将我的收藏设置为“封顶”时,它就起作用了 但出于很多原因,我不会使用封顶系列 无法删除文档(必须删除收藏) 无法通过编程

  • 本文向大家介绍SpringBoot2 实现JPA分页和排序分页的案例,包括了SpringBoot2 实现JPA分页和排序分页的案例的使用技巧和注意事项,需要的朋友参考一下 分页 application.yml 实体类 Repository接口类 service 接口类 Service 实现类 Controller控制器类 分页显示的json格式串 http://localhost:8080/emp

  • 本文向大家介绍SpringBoot2.X Devtools热部署实现解析,包括了SpringBoot2.X Devtools热部署实现解析的使用技巧和注意事项,需要的朋友参考一下 pom文件引入所需jar包 springboot版本采用2.3.1 启动设置成热部署 修改代码后点击刷新即可 控制台重新打印SpringBoot图标就可以了 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多

  • 我试图配置一个SpringBoot应用程序,将度量导出到InfluxDB,以便使用Grafana仪表板可视化它们。我使用这个仪表板作为一个例子,它使用普罗米修斯作为后端。对于一些度量,我不知道如何为它们创建图,但对于其他一些度量,我不知道如何创建图,甚至不知道是否可能。所以我在以下几点中列举了我不太确定的事情: > 是否有描述价值单位的文档?我作为示例使用的应用程序没有任何负载,所以有时我不知道值

  • 本文向大家介绍SpringBoot2使用WebFlux函数式编程的方法,包括了SpringBoot2使用WebFlux函数式编程的方法的使用技巧和注意事项,需要的朋友参考一下 本文只是简单使用SpringBoot2使用WebFlux的函数式编程简单使用,后续会继续写关于Webflux相关的文章。 最近一直在研究WebFlux,后续会陆续出一些相关的文章。 首先看一下Srping官网上的一张图,对比

  • 本文向大家介绍基于springboot2集成jpa,创建dao的案例,包括了基于springboot2集成jpa,创建dao的案例的使用技巧和注意事项,需要的朋友参考一下 springboot中集成jpa需要再pom文件中添加jpa的jar包,使用springboot的话iju不用自己规定版本号了,自动管理依赖版本即可。 然后我们再添加hibernate和oracle的jar包,同样自动管理版本。