【发布时间】:2017-10-28 18:18:02
【问题描述】:
我有一个使用 Spring Boot、Angular 2、Spring OAuth 2 的系统,我在同一个应用程序中使用 @EnableWebSecurity 实现了安全性,并使用 @EnableResourceServer 和 @EnableAuthorizationServer 实现了 oauth。
以下是实现的类:
SecurityConfig.java
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("pass").roles("USER").and()
.withUser("username").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/private/**").hasRole("USER")
.antMatchers("/public/**").permitAll();
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
AuthorizationServerConfig.java
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my-trusted-client")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT","USER")
.scopes("read", "write", "trust")
.secret("secret")
.accessTokenValiditySeconds(1200).
refreshTokenValiditySeconds(6000);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.checkTokenAccess("hasAuthority('USER')");
}
}
ResourceServerConfig.java
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/public/**").permitAll();
http.authorizeRequests().antMatchers("/private/**").hasRole("USER");
}
}
/public 之后的所有 url 都可以被任何用户访问;这是正确的。 /private/ 后面的 url 由 ResourceServerConfig 和 SecurityConfig 保护,因此匿名用户无法访问。
当我使用grant_type=password 从授权服务器请求access_token 时,我通过附加access_token 作为参数获得了用于访问受保护资源的access_token。但是资源仍然不可用,我得到如下响应:
localhost:8080/private/user/test/?access_token=92f9d86f-83c4-4896-a203-e21976d4cfa2
{
"timestamp": 1495961323209,
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/private/user/test/"
}
但是当我从SecurityConfig.configure(HttpSecurity) 中删除antMatchers 时,即使ResourceServerConfig.configure(HttpSecurity) 正在保护模式,资源也不再受到保护。
我的问题:
- 我需要在
ResourceServerConfig中执行什么操作才能从资源服务器向授权用户授予访问权限? -
@EnableResourceServer和@EnableWebSecurity有什么区别?我需要在这个应用程序中实现这两者吗? (我找不到这个问题的任何好的答案)
【问题讨论】:
-
你有解决上述问题的办法吗?
标签: java spring spring-boot spring-security oauth-2.0