从数据库获取oAuth2安全凭据的Spring Boot Rest服务

8

请问有没有一个包含使用oAuth2保护Spring Security的端点,并且使用来自MySQL数据库的用户凭据的Spring Boot应用程序的示例?

2个回答

5

谢谢Dave。我对Spring Security还是很新的。Spring Boot使创建RESTful服务变得超级简单,但我仍然不清楚如何使用oAuth2和数据库凭据实现安全性。我看到一些早期的Spring Boot项目在DB中创建令牌和刷新表。这不再是定制吗?https://github.com/royclarkson/spring-rest-service-oauth使用内存身份验证。这比将令牌存储在DB中更受欢迎吗? - Hardy Le Roux
1
该链接包含了你提到的模式的SQL文件(它们在内存数据库启动时被执行,但你也可以自己为MySQL执行它们)。Roy的示例类似(同样很简单),但显然对于大多数系统来说,内存用户存储在生产中并不实用。 - Dave Syer

0
请参考https://github.com/royclarkson/spring-rest-service-oauth/并进行以下更改,它使用在application.properties中定义的主数据源。
@Configuration
public class OAuth2ServerConfiguration {

    private static final String RESOURCE_ID = "rest_api";

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends
            ResourceServerConfigurerAdapter {

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

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/users").hasRole("ADMIN")
                    .antMatchers("/review").authenticated()
                    .antMatchers("/logreview").authenticated()
                    .antMatchers("/oauth/token").authenticated()
                    .and()
                    .csrf()
                    .csrfTokenRepository(csrfTokenRepository()).and()
                    .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
                    ;
            }

            private Filter csrfHeaderFilter() {
                return new OncePerRequestFilter() {

                    @Override
                    protected void doFilterInternal(HttpServletRequest request,
                            HttpServletResponse response, FilterChain filterChain)
                            throws ServletException, IOException {

                        CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
                                .getName());
                         if (csrf != null) {
                            Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                            String token = csrf.getToken();
                            if (cookie == null || token != null
                                    && !token.equals(cookie.getValue())) {
                                cookie = new Cookie("XSRF-TOKEN", token);
                                cookie.setPath("/");
                                response.addCookie(cookie);
                            }
                        }
                        filterChain.doFilter(request, response);
                    }
                };
            }
            private CsrfTokenRepository csrfTokenRepository() {
                HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
                repository.setHeaderName("X-XSRF-TOKEN");
                return repository;
            }
        }


    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationServerConfiguration extends
            AuthorizationServerConfigurerAdapter {


        @Autowired
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;

        @Autowired
        DataSource dataSource;


        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {

            endpoints
                .tokenStore(new JdbcTokenStore(dataSource))
                .authenticationManager(this.authenticationManager);

        }

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

        @Bean
        @Primary
        public DefaultTokenServices tokenServices() {
            DefaultTokenServices tokenServices = new DefaultTokenServices();
            tokenServices.setSupportRefreshToken(true);
            tokenServices.setAccessTokenValiditySeconds(300);
            tokenServices.setRefreshTokenValiditySeconds(6000);
            tokenServices.setTokenStore(new JdbcTokenStore(dataSource));
            return tokenServices;
        }


    }
}

嗨,我是新手,但由于某些原因,我不知道如何处理POST请求。{"error":"access_denied","error_description":"Expected CSRF token not found. Has your session expired?"}使用GET方法正常。 - Murtaza Kanchwala
你遇到了同样的问题,你找到解决方案了吗? - robert trudel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接