The*_*der 5 java spring spring-security spring-boot spring-security-oauth2
我有一个受OAuth2.0保护的REST API,我可以使用http:// localhost:8085 / auth / token?grant_type=password&username=22@gmail.com&password=mypass来获取访问令牌(以及用户名通过基本身份验证)。
但是,当我尝试访问http:// localhost:8085 / api / v1 / signup时,API返回401 unauthorized错误。
尽管我使用过antMatchers("/signup").permitAll(),但为什么API期望A access-token访问此资源?access-token与此请求一起传递将注册用户。
这是我的资源服务器配置
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
//require beans and methods here
@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authProvider());
}
@Override
public void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/signup").permitAll()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable();
}
}
Run Code Online (Sandbox Code Playgroud)
更新:正如该线程所建议的那样,我/signup在``处忽略了,但这也没有用。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@ComponentScan(basePackages = { "com.sample.rest.security" })
@Order(2)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//other Beans & methods
@Override
protected void configure(HttpSecurity http) throws Exception {
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>();
requestMatchers.add(new AntPathRequestMatcher("/signup/**"));
http.
requestMatcher(new OrRequestMatcher(requestMatchers)).
authorizeRequests().antMatchers("/signup/**")
.permitAll();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道了 导致问题的原因是上下文路径。我有一个使用映射URL定义的调度程序servlet /api/v1/*,正如我所看到的signup,它包含一个上下文路径,即 http://localhost:8085/api/v1/signup
对于Spring中的OAuth2配置,我们需要特别注意上下文路径。首先,应在AuthorizationServer中定义
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.prefix("/api/v1") //here
.tokenStore(tokenStore())
.accessTokenConverter(accessTokenConverter())
.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
Run Code Online (Sandbox Code Playgroud)
然后,必须将上下文添加到这样的permitAll()路径中
@Override
public void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/v1/signup").permitAll() //context path here
.anyRequest().authenticated();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,仍希望注册请求将访问令牌与它一起传递。要从注册中删除OAuth安全性,我们需要在处删除安全性WebSecurity,可以使用来完成WebSecurityConfigurerAdapter
@EnableWebSecurity
@EnableGlobalMethodSecurity
@ComponentScan(basePackages = { "com.sample.rest.security" })
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/signup");
}
//////////// OR use below method ///////////
/* @Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests().antMatchers("/signup/**").permitAll();
}
*/
}
Run Code Online (Sandbox Code Playgroud)
注意,没有必要将上下文路径添加到WebSecurityConfigurerAdapter配置中。
| 归档时间: |
|
| 查看次数: |
2358 次 |
| 最近记录: |