gpt4 book ai didi

java - 将文本框值设置为发布到 j_spring_security_check 的 JSP 表单中的 session 变量

转载 作者:行者123 更新时间:2023-12-01 12:37:17 25 4
gpt4 key购买 nike

我有index.jsp,它发布到`j_spring_security_check。我正在尝试将文本框值设置为 session 变量。

下面是我的index.jsp

<form id="loginForm" name="loginForm" method="post" action="j_spring_security_check" autocomplete="off">
<label>email address</label>
<input type="text" name="j_username" id="username" autocorrect="off" autocapitalize="off" autocomplete="off" placeholder="email address" title="email address" />
<label>password</label>
<input type="password" name="j_password" id="password" autocorrect="off" autocapitalize="off" autocomplete="off" placeholder="password" title="password" />
<button type="submit" onclick="this.disabled=true; this.form.submit.click();"><spring:message code="v2.login.button" /></button>
</form>

我试图在 session 中设置用户名值,但表单发布到 j_spring_security_check,我真的不知道 spring security 是如何工作的,所以我不确定在哪里添加逻辑以将用户名值放入 session 中。我想知道是否可行,如果可以的话,我应该在哪里添加逻辑?

解决方案:当登录表单发布到/j_spring_security_check UsernamePasswordAuthenticationFilter 时,调用对用户进行身份验证。以下是将用户名放入 session 中的更改 在 security-applicationContext.xml 中添加了如下的 customUsernamePasswordAuthenticationFilter

<beans:bean id="customUsernamePasswordAuthenticationFilter" class="com.datacert.core.security.filter.CustomUsernamePasswordAuthenticationFilter" >
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationFailureHandler" ref="DCAuthenticationFailureHandler" />
<beans:property name="authenticationSuccessHandler" ref="DCAuthenticationSuccessHandler" />
</beans:bean>

并创建了一个在 session 中设置用户名的类

@Primary
public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

public CustomUsernamePasswordAuthenticationFilter() {
super();
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
/*
* String username = super.obtainUsername(request); if (username != null && !StringUtils.isEmpty(username)) {
* HttpSession session = request.getSession(); session.setAttribute("username", username); }
*/
return super.attemptAuthentication(request, response);

}
}

最佳答案

在 Spring security 中,身份验证对象保存在 SecurityContext 中.

获取Authentication对象从 org.springframework.security.core.context.SecurityContextHolder#getContext() 获取它

SecurityContext securityContext = SecurityContextHolder.getContext();
Object principal;
String username;
if(null != securityContext.getAuthentication()){
principal = securityContext.getAuthentication().getPrincipal();
username = securityContext.getAuthentication().getName();
}

username的值将是身份验证中使用的用户名。principal的值将成为主要对象。许多身份验证提供程序将创建一个 UserDetails 对象作为主体。

引用号:SecurityContext , AuthenticationSecurityContextHolder

关于java - 将文本框值设置为发布到 j_spring_security_check 的 JSP 表单中的 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25476707/

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