使用自定义UserDetailService使用JDBC Spring Boot OAuth2

时间:2016-11-25 03:24:12

标签: spring-security spring-boot oauth-2.0 spring-security-oauth2 spring-security-rest

我正在尝试使用带有Spring安全性的OAuth2.0保护我的Spring Boot Rest API,并希望在JDBC数据库中存储OAuth令牌(访问令牌和刷新令牌)。 对于用户名和密码验证,我创建了CustomUserDetailService。

我收到以下错误 -

<script type="text/javascript"> 
$(document).ready(function(){ 
$('.p1, .p2, .p3, .p4').hide(); 
$('.pageSubtitle').click(function(){ 
$(this).nextUntil('.pageSubtitle').toggle();
 }); 
 }); </script>

我的代码是 -

Oauth2Config

{
  "timestamp": 1480042650103,
  "status": 401,
  "error": "Unauthorized",
  "message": "Bad credentials",
  "path": "/oauth/token"
}

WebSecurityConfig

@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class AuthorizationServerConfiguration {

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Autowired
        private TokenStore tokenStore;


        @Override
        public void configure(ResourceServerSecurityConfigurer resources) {
            resources.tokenStore(tokenStore);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
            .anonymous().disable()
            .authorizeRequests().anyRequest().authenticated();
        }
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2ServerConfiguration extends AuthorizationServerConfigurerAdapter {

        @Autowired
        private AuthenticationManager authenticationManager;

        @Autowired
        private DataSource dataSource;

        private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        @Autowired
        private CustomUserDetailService customUserDetailService;

        @Bean
        public JdbcTokenStore tokenStore() {
            return new JdbcTokenStore(dataSource);
        }

        @Bean
        protected AuthorizationCodeServices authorizationCodeServices() {
            return new JdbcAuthorizationCodeServices(dataSource);
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
            security.passwordEncoder(passwordEncoder);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                .authorizationCodeServices(authorizationCodeServices())
                .authenticationManager(authenticationManager)
                .tokenStore(tokenStore())
                .approvalStoreDisabled()
                .userDetailsService(customUserDetailService);
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
                .jdbc(dataSource);
        }

    }
}

CustomUserDetailService

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;


    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {  
        auth
        .userDetailsService(userDetailsService)
        .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}

application.properties

@Service
public class CustomUserDetailService 
    implements UserDetailsService {

    @Override
    public UserDetailsService loadUserByUsername(String username) throws UsernameNotFoundException {
        // I tested this logic and works fine so i avoid this lines
        return userDetailsService;
    }
}

请建议我如何解决此错误?

0 个答案:

没有答案