Spring Boot 1.3 Oauth2 Sso return 401 而不是重定向到授权服务器
Spring Boot 1.3 Oauth2 Sso return 401 instead of redirect to authorization server
在 Spring Boot 1.3 中,Spring Boot 中有 Oauth2 的自动配置。
有一个 spring guide 提供了一些很好的例子,但我想实现一个不同的解决方案。
我的问题基于提供的 click 示例。
我想在访问“/login”端点后被重定向到授权服务器。如果我在没有身份验证的情况下请求受保护的资源,我想获得 401(未授权)而不是立即重定向 (302) 到授权 uri。
这是点击示例的Java代码
@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {
@RequestMapping("/user")
public Principal user(Principal principal) {
return principal;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**")
.permitAll()
.anyRequest()
.authenticated();
}
public static void main(String[] args) {
SpringApplication.run(SocialApplication.class, args);
}
}
我尝试添加一个自定义的 AuthenticationEntryPoint,但它似乎被忽略了:(
我尝试了什么:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Session realm=\"JSESSIONID\""));
}
单击示例的完整源代码可以在 github.
上找到
是否可以实现 401 而不是重定向?
在 spring-boot 1.3.0 之前,由于使用此组合时应用自定义配置器的顺序存在问题,因此不可能这样做(参见 #4629 for further resolution)。
从 spring-boot 1.3.1 开始,请求 header X-Requested-With: XMLHttpRequest
表明调用者更喜欢 401 Unauthorized
而不是 302 Found
在 Spring Boot 1.3 中,Spring Boot 中有 Oauth2 的自动配置。
有一个 spring guide 提供了一些很好的例子,但我想实现一个不同的解决方案。 我的问题基于提供的 click 示例。 我想在访问“/login”端点后被重定向到授权服务器。如果我在没有身份验证的情况下请求受保护的资源,我想获得 401(未授权)而不是立即重定向 (302) 到授权 uri。
这是点击示例的Java代码
@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {
@RequestMapping("/user")
public Principal user(Principal principal) {
return principal;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**")
.permitAll()
.anyRequest()
.authenticated();
}
public static void main(String[] args) {
SpringApplication.run(SocialApplication.class, args);
}
}
我尝试添加一个自定义的 AuthenticationEntryPoint,但它似乎被忽略了:(
我尝试了什么:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("Session realm=\"JSESSIONID\""));
}
单击示例的完整源代码可以在 github.
上找到是否可以实现 401 而不是重定向?
在 spring-boot 1.3.0 之前,由于使用此组合时应用自定义配置器的顺序存在问题,因此不可能这样做(参见 #4629 for further resolution)。
从 spring-boot 1.3.1 开始,请求 header X-Requested-With: XMLHttpRequest
表明调用者更喜欢 401 Unauthorized
而不是 302 Found