user3495816
user3495816

Reputation: 637

Springboot2 and oauth

I am trying to make work oauth with Springboot2 using https://github.com/spring-projects/spring-security-oauth2-boot

with this tutorial: https://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/

SpringBootApp

@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();
    }
}

ServletInitializer

public class ServletInitializer extends SpringBootServletInitializer {

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

UserDetailsService

@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()));
    }
}

When trying to retrieve acces token:

http://localhost:8080/oauth/token

stack:

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

I tried to fixed the issues but now it won't start. What AuthenticationManager should I provide?

Upvotes: 0

Views: 1276

Answers (1)

rena
rena

Reputation: 1278

You need the following configuration in your config class and get rid of those two beans you have in your application class

@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);
    }

    //....
}

Upvotes: 1

Related Questions