Java Spring security Oauth 2在/Oauth/token上的重定向行为
我正在开发一个Spring Boot应用程序,我遇到的问题如下: 默认情况下,对我的控制器的每个请求都被重定向到/login,因此我不能这样使用它 我可以通过设置禁用/登录Java Spring security Oauth 2在/Oauth/token上的重定向行为,java,spring,spring-mvc,oauth,Java,Spring,Spring Mvc,Oauth,我正在开发一个Spring Boot应用程序,我遇到的问题如下: 默认情况下,对我的控制器的每个请求都被重定向到/login,因此我不能这样使用它 我可以通过设置禁用/登录 security.oauth2.resource.filter-order = 3 在application.properties中 但我想知道如何仅对非浏览器禁用它,因此我仍然可以使用登录 编辑: 这就是我的方法: http.antMatcher("/api/**") .author
security.oauth2.resource.filter-order = 3
在application.properties中
但我想知道如何仅对非浏览器禁用它,因此我仍然可以使用登录
编辑:
这就是我的方法:
http.antMatcher("/api/**")
.authorizeRequests().anyRequest().authenticated();
http.antMatcher("/**")
.formLogin();
从/api/运行的所有内容都是REST,应该仅由Oauth处理。
其余的应该通过登录来处理
但它仍然不工作,我仍然得到重定向
没关系,修好了。谢谢你的长号码
在我的例子中,我的Web安全性被错误配置了。
现在可以这样做了:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
formLogin().permitAll()
.and()
.authorizeRequests().anyRequest().authenticated();
}
我的RessourceServerConfig如下所示:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(customAuthenticationEntryPoint)
.and()
.logout()
.logoutUrl("/oauth/logout")
.logoutSuccessHandler(customLogoutSuccessHandler)
.and()
.headers()
.frameOptions().disable()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/**").authenticated();
}
}
您需要降低主应用程序安全筛选器的优先级 如Spring引导教程中所述 @EnableResourceServer注释创建了一个安全过滤器,默认情况下,过滤器的顺序为@OrderSecurityProperties.ACCESS\u OVERRIDE\u-ORDER-1,因此通过将主应用程序安全性移动到@OrderSecurityProperties.ACCESS\u OVERRIDE\u-ORDER,我们可以确保OAuth资源的规则优先 背景 security.oauth2.resource.filter-order=3 不禁用主安全筛选器。它只是将优先级更改为较低的值。负值具有最高优先级-3的优先级高于3 据我所知,您的问题是没有在ResourceServerConfiguration中正确设置matcher。具体如下
http.antMatcher("/helloOAuth")
.authorizeRequests().anyRequest().authenticated();
有一个示例实现。可能会有帮助