我有一个使用 Spring Boot、Angular 2、Spring OAuth 2 的系统,我使用 @EnableWebSecurity
实现了安全性,并使用 @EnableResourceServer
和 @ 实现了 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
作为参数。但是资源仍然不可用,我得到如下响应:
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
有什么区别?我是否需要在此应用程序中同时实现两者? (我找不到这个问题的任何好的答案)
最佳答案
您的私有(private)资源已得到很好的保护,但获得的access_token 没有以正确的方式传递给服务器。
你必须将它作为请求的 header 传递给
Authorization: Bearer 92f9d86f-83c4-4896-a203-e21976d4cfa2
或作为 curl 命令:
curl -H "Authorization: Bearer 92f9d86f-83c4-4896-a203-e21976d4cfa2"
关于java - Spring security 的@EnableWebSecurity 与 oauth 的@EnableResourceServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225544/