如何使用外部调用注销 Spring 安全中的用户
How to logout User in Spring Security with external call
我有一个 Spring 启动应用程序,其中实施了 spring 安全性。用户登录和注销成功运行,我正在使用 jdbc 进行会话存储。
现在我只想实现一个新的休息端点,它接受特定用户的用户名作为参数并注销该用户。
目前我正在使用 follwing class 作为注销实现。
@Component
public class CustomLogoutHandler extends SecurityContextLogoutHandler {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
super.logout(request, response, authentication);
}
}
但是我的问题是如何使用此 class 或内置 Spring 注销从外部请求中注销用户?
session_store 我使用以下配置,用户会话已成功保存在数据库中。
#Spring Session
spring.session.store-type=jdbc
spring.session.jdbc.table-name= SESSION_TABLE
从 table 中手动删除 table 条目可以是 done.And 它将注销用户。
但我想按照 Spring 安全的正确流程进行注销。
谢谢
以下是我的处理方式:
- 为注销创建会话范围的控制器
- 创建端点并将
HttpSession
注入其中
- 调用端点后使会话无效
类似于:
@Scope("session")
@Controller
public class LogoutController {
@PostMapping("/logout")
public ResponseEntity logout(HttpSession session) {
session.invalidate();
return new ResponseEntity().ok();
}
}
此外,我不确定您为什么不使用 Spring:
已经提供的注销配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// config your credentials provider here
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
// protected endpoints config here
.and()
.logout().logoutUrl("/logout")
.and().csrf().disable();
}
}
并且您可以使用 POST 直接调用 /logout
,这将触发注销。
我认为您可以使用 spring 会话来注销特定用户,只需在您的控制器中自动装配会话存储库即可。
如果我很清楚你已经配置了一个 JdbcOperationsSessionRepository,那么你可以这样做:
@Autowired
private org.springframework.session.jdbc.JdbcOperationsSessionRepository sessionRepository;
然后使用您的存储库查找与特定用户相关的会话
sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, somePrincipalName);
我有一个 Spring 启动应用程序,其中实施了 spring 安全性。用户登录和注销成功运行,我正在使用 jdbc 进行会话存储。 现在我只想实现一个新的休息端点,它接受特定用户的用户名作为参数并注销该用户。 目前我正在使用 follwing class 作为注销实现。
@Component
public class CustomLogoutHandler extends SecurityContextLogoutHandler {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
super.logout(request, response, authentication);
}
}
但是我的问题是如何使用此 class 或内置 Spring 注销从外部请求中注销用户? session_store 我使用以下配置,用户会话已成功保存在数据库中。
#Spring Session
spring.session.store-type=jdbc
spring.session.jdbc.table-name= SESSION_TABLE
从 table 中手动删除 table 条目可以是 done.And 它将注销用户。 但我想按照 Spring 安全的正确流程进行注销。
谢谢
以下是我的处理方式:
- 为注销创建会话范围的控制器
- 创建端点并将
HttpSession
注入其中 - 调用端点后使会话无效
类似于:
@Scope("session")
@Controller
public class LogoutController {
@PostMapping("/logout")
public ResponseEntity logout(HttpSession session) {
session.invalidate();
return new ResponseEntity().ok();
}
}
此外,我不确定您为什么不使用 Spring:
已经提供的注销配置@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// config your credentials provider here
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
// protected endpoints config here
.and()
.logout().logoutUrl("/logout")
.and().csrf().disable();
}
}
并且您可以使用 POST 直接调用 /logout
,这将触发注销。
我认为您可以使用 spring 会话来注销特定用户,只需在您的控制器中自动装配会话存储库即可。 如果我很清楚你已经配置了一个 JdbcOperationsSessionRepository,那么你可以这样做:
@Autowired
private org.springframework.session.jdbc.JdbcOperationsSessionRepository sessionRepository;
然后使用您的存储库查找与特定用户相关的会话
sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, somePrincipalName);