我正在开发 springboot 3 应用程序。我必须使用 Jwt 对象,因此我必须导入此依赖项以及
spring-boot-starter-security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
我有一个自定义的
/logout
休息端点。
现在,有了这个新的依赖项,当我调用端点时,它会返回 302
代码并重定向到 /login?logout
。
因此,我更改了配置,如下所示,使其调用自定义休息端点,但它仅返回带有重定向 URL 的
302
。
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request -> request
.requestMatchers("/login*")
.permitAll()
.anyRequest()
.permitAll())
.logout(logout -> logout.logoutSuccessUrl("/cms/logout").permitAll());
http.httpBasic(AbstractHttpConfigurer::disable);
return http.build();
}
我不希望它重定向到另一个 URL,我只想调用我的自定义注销休息端点。
我尝试了以下方法,但它不起作用
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request -> request
.requestMatchers("/login*")
.permitAll()
.anyRequest()
.permitAll())
.logout(logout -> logout.permitAll()
.logoutSuccessHandler(((request, response, authentication) -> new CustomLogoutHandler())));
http.httpBasic(AbstractHttpConfigurer::disable);
return http.build();
}
private class CustomLogoutHandler implements LogoutSuccessHandler{
@Autowired
AuthServiceImpl authService;
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
authService.cmslogout(request,response);
}
}
如果这个 spring-security 注销处理可以整体禁用,那就更好了。
请帮忙。
登录和注销在 OAuth2 资源服务器上都没有意义。这是 OAuth2 client 的问题。请参阅我的教程的OAuth2 要点部分。
如果这个 spring-security 注销处理可以整体禁用,那就更好了。
只提供任何相关内容(没有处理程序,没有成功处理程序,没有端点,什么都没有)。
此外,请参阅资源服务器教程内部,以向未经授权的请求返回正确的
401
(而不是302
):
http.exceptionHandling(eh -> eh.authenticationEntryPoint((request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
}));