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

标签 java jsp session

我有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/

相关文章:

java - 使用java在mongodb中获取最大/最小值(没有字段名称)

java - 如何在 BlackBerry 中关闭全局对话框并显示另一个对话框?

java - 从JSP访问资源文件

PHP 和 MySQL session

java - CipherOutputStream 创建 0 字节文件

java - HTTP 服务器问题

java - 表达式语言,不显示变量值

java - 比较 Struts2 中的字符串 <s :if> Tag

php - 在 PHP session 中存储多个值

PHP session_set_cookie_params 中断 php session_start() 函数和 $_SESSION 变量