当前位置: 首页 > 知识库问答 >
问题:

如何通过Spring安全登录页面传递附加参数

金成济
2023-03-14

我正在尝试将数据库名称设置为Spring Security登录页面的请求输入参数。目前,我只获得使用Spring安全安全上下文持有者检索的用户名。

如何访问登录页面上设置的附加字段?

共有3个答案

华化
2023-03-14

Source cedelica提到使用AuthenticationDetailsSource和自定义的Authentication详细信息。这是一个例子。

将bean id为< code > customWebAuthenticationDetailsSource 的< code > authentic ation-details-source-ref 属性添加到< code>form-login中:

<security:http>
    <security:intercept-url pattern="/**" access="..." />
    <security:form-login authentication-details-source-ref="customWebAuthenticationDetailsSource" login-page="..." />
    <security:logout logout-success-url="..." />
</security:http>

创建新类CustomWebAuthenticationDetailsSource

package security;

import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;

public class CustomWebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {
    @Override
    public WebAuthenticationDetails buildDetails(HttpServletRequest context) {
        return new CustomWebAuthenticationDetails(context);
    }
}

以及相关的CustomWebAuthenticationDetails:

package security;

import org.springframework.security.web.authentication.WebAuthenticationDetails;
import javax.servlet.http.HttpServletRequest;

public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {

    private final String yourParameter;

    public CustomWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        yourParameter = request.getParameter("yourParameter");
    }

    public String getyourParameter() {
        return yourParameter;
    }

    //TODO override hashCode, equals and toString to include yourParameter
    @Override
    public int hashCode() { /* collapsed */ }
    @Override
    public boolean equals(Object obj) { /* collapsed */ }
    @Override
    public String toString() { /* collapsed */ }
}
孙朗
2023-03-14

阐述@Vacuum的评论

这里有一个简单的方法(未经测试,但我相信这是可行的)

  1. 创建一个新类 ExUsernamePassword 身份验证筛选器,该类将扩展默认筛选器并获取其他参数并将其存储在会话中。它看起来像这样:
public class ExUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        final String dbValue = request.getParameter("dbParam");
        request.getSession().setAttribute("dbValue", dbValue);

        return super.attemptAuthentication(request, response); 
    } 
}
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;

以获取步骤 1) 中的筛选器提供的会话变量。

xml prettyprint-override"><custom-filter ref="beanForYourCustomFilterFromStep1" position="FORM_LOGIN_FILTER"/>

有关自定义过滤器的更多信息,请参阅留档的这一部分:http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-custom-filters

白成济
2023-03-14

有很多方法可以做到这一点,但官方的方法是使用定制的AuthenticationDetailsAuthenticationDetailsSourceSpring的 WebAutheenticationDetails Source。将额外字段添加到自定义 WebAuthenticationDetails中,并让自定义的 WebAutheenticationDetails源

在Spring Security 3.1中,很容易通过使用< code >的< code > authentic ation-details-source-ref 属性进行配置

在 3.0 中,您必须使用 BeanPost 处理器。Spring安全常见问题解答中有一个关于使用 BeanPost 处理器配置自定义 Web 身份验证详细信息来源的示例。

完成此操作后,您可以调用安全上下文持有者.getContext().getAuthication(.getDetails() 来访问您的额外字段。

 类似资料:
  • 问题内容: 我正在尝试从Spring安全性登录页面将数据库名称设置为请求输入参数。目前,我只获得使用spring security检索到的用户名。 如何访问在登录页面上设置的其他字段? 问题答案: 详细阐述@Vacuum的评论 这是一种简单的方法(未经测试,但我相信这会起作用) 1)创建一个新类,它将扩展默认过滤器并获取其他参数并将其存储在会话中。它看起来像这样: 2)在你的实现中,修改你的实现:

  • 我正在做一个项目,其中为登录身份验证实现了Spring Security。现在,我们希望当用户登录时,它连接到哪个数据库取决于一个参数,即我在登录过程中传递的参数,并希望在页面上访问该参数home.jsp我的代码为: 1)登录页面url /登录 2)Spring表单登录为 3) 访问决策管理器类 网址为家.jsp /首页 有人能帮我吗?我如何将一个额外的参数从登录页面传递到主页。如果我在登录前进行

  • 通过设置安全码,从而达到隐藏登录页的效果,保证页面的私有性。 /app/Application/Admin/Conf/config.php return array( 'LOGIN_MAX_FAILD' => 5, 'BAN_LOGIN_TIME' => 30, //登录失败5次之后需等待30分钟才可再次登录 'ADMIN_PANEL_SECURITY_CODE' => '

  • 在通过Maven运行时,我需要传递给Karate的附加设置可以在karate-config.js中获得。目前,我可以使用karate.env属性传入一个字符串--是否需要将参数编码为JSON对象并通过这个属性传入,或者我可以做如下操作: 我肯定我错过了一些明显的东西...

  • 我使用Spring MVC和Spring Security进行认证。我用用户名和密码登录,但我想再发送两个参数。有什么办法可以做到吗? 这是登录表单: 我也想在登录中发送时区和语言。 以及

  • 我想通过数组传递有效和无效凭证来自动登录,但问题是,每次传递值后,登录按钮都会继续加载,也就是说,它会在值1之后继续加载,但即使在这种情况下,值2也会被传递。我甚至试图延迟每个值的时间,但它不起作用。我需要检查按钮是否一直处于加载状态吗?或者还有其他方法吗?