来自数据库的具有oAuth2安全凭据的Spring引导Rest服务
有谁能帮我举一个Spring引导应用程序的例子,它包含一个Rest服务,其端点使用oAuth2和MySQL数据库中的用户凭据受Spring安全性保护 这个怎么样:(不是MySQL,而是JDBC,所以转换很简单)?请参考 并执行以下更改,它使用application.properties中定义的主数据源来自数据库的具有oAuth2安全凭据的Spring引导Rest服务,rest,jpa,oauth-2.0,spring-boot,Rest,Jpa,Oauth 2.0,Spring Boot,有谁能帮我举一个Spring引导应用程序的例子,它包含一个Rest服务,其端点使用oAuth2和MySQL数据库中的用户凭据受Spring安全性保护 这个怎么样:(不是MySQL,而是JDBC,所以转换很简单)?请参考 并执行以下更改,它使用application.properties中定义的主数据源 @Configuration public class OAuth2ServerConfiguration { private static final String RESOURCE_
@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;
}
}
}
谢谢你,戴夫。我对spring security还是个新手。SpringBoot使创建RESTful服务变得非常容易,但我仍然不清楚使用oAuth2和数据库凭据实现安全性。我看到一些Spring之前的启动项目,它们在DB中创建令牌和刷新表。这不再是习俗了吗?使用内存中的身份验证。上面是不是最好将令牌存储在DB中?该链接包含您提到的模式的SQL文件(它们在内存数据库启动时执行,但您可以自己为mysql执行)。Roy的示例与之类似(并且同样最小),但内存中的用户存储显然不会在大多数系统的生产中有用。嗨,我对这一点不熟悉,但出于某些原因,我不适合POST请求。{“error”:“access_denied”,“error_description”:“未找到预期的CSRF令牌。您的会话是否已过期?”}GET方法工作正常同样的问题,您找到解决方案了吗?