gpt4 book ai didi

java - Spring Security 的编程使用

转载 作者:IT老高 更新时间:2023-10-28 13:05:03 26 4
gpt4 key购买 nike

我正在使用 Wicket我的表示层使用了 Wicket Auth Project,因此我将它与 Spring Security 集成。这是 Wicket 为我调用的身份验证方法:

@Override
public boolean authenticate(String username, String password) {
try {
Authentication request = new UsernamePasswordAuthenticationToken(
username, password);
Authentication result = authenticationManager.authenticate(request);
SecurityContextHolder.getContext().setAuthentication(result);
} catch (AuthenticationException e) {
return false;
}
return true;
}

我的 Spring Security XML 配置的内容(里面)是:

<http path-type="regex">
<form-login login-page="/signin"/>
<logout logout-url="/logout" />
</http>
<global-method-security secured-annotations="enabled" />
<authentication-manager alias="authenticationManager"/>
<authentication-provider user-service-ref="userService">
<password-encoder ref="bcryptpasswordencoder" />
</authentication-provider>

该部分2.3.6. Session Fixation Attack Protection引用文档中说:

Session fixation attacks are a potential risk where it is possible for a malicious attacker to create a session by accessing a site, then persuade another user to log in with the same session (by sending them a link containing the session identifier as a parameter, for example). Spring Security protects against this automatically by creating a new session when a user logs in. If you don't require this protection, or it conflicts with some other requirement, you can control the behaviour using the session-fixation-protection attribute on , which has three options:

  • migrateSession - creates a new session and copies the existing session attributes to the new session. This is the default.
  • none - Don't do anything. The original session will be retained.
  • newSession - Create a new "clean" session, without copying the existing session data.

身份验证有效,但我是 Spring Security 的新手,我也有一些问题需要回答:

  • 通常登录时,我会将身份验证信息发布到 j_spring_security_check 并让 Spring Security 执行实际的身份验证代码。我想要保护免受 session 固定攻击,当我像我一样执行程序登录时会得到它吗?如果没有,我该怎么做才能得到它?
  • 如何执行程序化注销?
  • 由于我将使用编程登录和注销,如何禁止 Spring 拦截这些 URL?

更新:对于 session 固定攻击保护,似乎我需要使用签名调用 SessionUtils 类中的方法 startNewSessionIfRequired(HttpServletRequest request, boolean migrateAttributes, SessionRegistry sessionRegistry)

如何获取需要传入的 SessionRegistry 实例?我找不到任何方法为其创建别名 ID,或者如何获取它的 ID 或名称。

最佳答案

也许这不是对您问题的完整答案,但也许会对您有所帮助。

当您不使用程序登录时调用的代码,但可以在此处找到标准代码:

org.springframework.security.ui.webapp.AuthenticationProcessingFilter

我猜你在你的代码中受到了启发。看起来很相似。

同样地,您在标准方法中访问 /j_spring_security_logout 时执行的代码也可以在这里找到:

org.springframework.security.ui.logout.LogoutFilter

LogoutFilter 调用多个处理程序。我们正在使用的处理程序称为:org.springframework.security.ui.logout.SecurityContextLogoutHandler,所以你可以在你的方法中调用相同的代码。

关于java - Spring Security 的编程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1013032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com