gpt4 book ai didi

java - 为什么 Spring Security permitAll() 不适用于 OAuth2.0?

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:27 25 4
gpt4 key购买 nike

我有一个使用 OAuth2.0 保护的 REST API我可以使用 http://localhost:8085/auth/token?grant_type=password&username=22@gmail.com&password=mypass 获取访问 token (与用户名一起通过基本身份验证)。
但是当我尝试访问 http://localhost:8085/api/v1/signup ,API 返回一个 401 unauthorized 错误。
虽然我使用了 antMatchers("/signup").permitAll(),但为什么 API 需要一个 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();
}
}

更新:根据this的建议线程,我忽略了 `` 处的 /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();
}

}

最佳答案

我明白了。这是导致问题的上下文路径。我有一个用映射 URL /api/v1/* 定义的调度程序 servlet,并且可以看到我的 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);
}

然后,上下文必须像这样添加到permitAll()路径

@Override
public void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/v1/signup").permitAll() //context path here
.anyRequest().authenticated();
}

到目前为止,注册请求仍然需要传递一个访问 token 。为了从注册中删除 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();
}
*/
}

请注意,将上下文路径添加到 WebSecurityConfigurerAdapter 配置是没有用的。

关于java - 为什么 Spring Security permitAll() 不适用于 OAuth2.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451123/

25 4 0
文章推荐: java - Controller 映射未在启动时记录
文章推荐: java - Scala List[Int] 在 Java 中变成 List