如何在使用LDAP进行身份验证的项目中正确配置Spring Security的“记住我”选项?

安德里亚·诺比利(AndreaNobili)

我在Spring MVC中还很陌生,尝试理解如何在登录页面中实现“记住我”功能时遇到了一些问题。

所以我正在一个使用4.1.7.RELEASE版本的Spring的Spring项目中该Web应用程序使用Spring Security和LDAP来处理用户登录。

进入这个项目,我有一个名为login.jsp的登录页面,这个页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);
%>

<!DOCTYPE html>
<html>
<head>
<!-- link href="<c:url value="resources/css/style.css" />" rel="stylesheet"-->

<link href="<c:url value="resources/css/bootstrap.css" />"
    rel="stylesheet">
<link href="<c:url value="resources/css/bootstrap-theme.css" />"
    rel="stylesheet">

<link href="<c:url value="resources/css/login.css" />" rel="stylesheet">

<title>Login Page</title>



</head>

<body onload='document.loginForm.username.focus();'>

    <!-- div id="intestazione">
        <h1 align="center">WIFI e PNSD</h1>
    </div-->

    <div class="container">
        <section id="sezioneBenvenuto">
            <h1 id="benvenuto" >Benvenuto</h1>
        </section>

        <section id="sezioneLogo" >
            <img src="resources/img/logo2.png" id="logo">
        </section>
        <section id="sezioneLogin">
            <div id="login-box">

                <c:if test="${not empty error}">
                    <div class="error">${error}</div>
                </c:if>

                <form class="form-horizontal" name='loginForm' action="<c:url value='/j_spring_security_check' />" method='POST'>
                    <div class="form-group" style="margin: 0px;" >
                        <label class="sr-only" for="exampleInputEmail3">Email address</label>
                        <input type='text' name='username' class="form-control" id="exampleInputEmail3" placeholder="Nome Utente">
                        <br>

                    </div>

                    <div class="form-group" style="margin: 0px;" >
                        <label class="sr-only" for="exampleInputPassword3">Password</label>
                        <input type='password' name='password' class="form-control" id="exampleInputPassword3" placeholder="Password">
                        <br>
                    </div>

                    <!--  <button type="submit" class="btn btn-default">Sign in</button> -->
                    <input id="ricorda" name="submit" type="submit" class="btn btn-default" value="Accedi" style="color: #0F8BB0;" />

                    <br>

                    <div style="white-space: nowrap; padding-top: 35px;">
                        <a id="linkGestioneUtenza" class="pull-left" href="${linkGestioneUtenza}">Gestione utenza</a>
                        <div class="pull-right">
                            <input style="vertical-align: top;" type="checkbox">
                            <label style="vertical-align: top; font-weight: normal; color: #0F8BB0;">Ricordami</label>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
    <!--jsp:include page="footer.jsp" /-->

    <script type="text/javascript" src="webjars/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="webjars/jquery/2.1.1/jquery.min.js"></script>

</body>
</html>

因此,正如您在前面的代码片段中所看到的,此复选框代表“记住我”的选择:

<div class="pull-right">
    <input style="vertical-align: top;" type="checkbox">
    <label style="vertical-align: top; font-weight: normal; color: #0F8BB0;">Ricordami</label>
</div>

好的,使用上一个login.jsp页面,我可以使用我的凭据访问我的应用程序没有问题,但是,如果我选中“记住我”复选框,它将无法正常工作(实际上,当我再次访问该应用程序时,我必须再次插入我的证书)。

因此,我阅读了官方文档,发现必须通过向浏览器发送cookie来实现或配置此行为,该cookie在以后的会话中会被检测到并导致自动登录,如此处所述:http:// docs.spring.io/spring-security/site/docs/4.1.x-SNAPSHOT/reference/html/remember-me.html

据我了解,有两种方法可以做到这一点:

  1. 第一个使用散列来保留基于cookie的令牌的安全性

  2. 第二种使用数据库或其他持久性存储机制来存储生成的令牌

这里是第一个疑问:这些断言究竟意味着什么?这两种方法有什么区别?

在文档中,我还可以阅读:

请注意,这两个实现都需要UserDetailsS​​ervice如果您使用的身份验证提供程序不使用UserDetailsS​​ervice(例如LDAP提供程序),那么它将不起作用,除非您应用程序上下文中还具有UserDetailsS​​ervice Bean

好的,在此项目中,使用LDAP进行用户身份验证在阅读文档时,在我看来UserDetailsS​​ervice是一个加载用户特定数据的接口(是否必须由自定义具体类实现?)。

因此,现在我的疑问是:该UserDetailsS​​ervice到底有什么作用我在哪里声明它可以将其放入我的应用程序上下文中进入Spring Security配置文件(spring-security.xml)还是进入Spring配置文件(root-context.xml)?

阿努迪普·加德(Anudeep Gade)

这些断言究竟意味着什么?

基本上记得我功能可以做两件事

  1. 成功登录后,它将使用基于用户名,密码的令牌创建一个“记住我”的浏览器cookie(如果需要,可以计算令牌并将其持久化在db中,也可以即时计算用户,密码的哈希值)。
  2. 当用户尝试访问没有任何会话的安全页面(会话由于超时而过期)时,RememberMe服务将尝试通过逆转该过程来自动登录用户(获取Cookie并使用userDetails哈希值进行检查或使用db中的持久化令牌进行检查)

这两种方法有什么区别?

TokenBasedRememberMeServices -

  1. 它使用用户详细信息的哈希来保留基于cookie的令牌的安全性。
  2. 它包含密码的哈希,如果捕获了cookie,则此解决方案可能很容易受到攻击。

PersistentTokenBasedRememberMeServices

  1. 它使用数据库或其他持久性存储机制来存储生成的令牌。
  2. 它为用户使用唯一的系列标识符。这标识了用户的初始登录,并在该持久会话期间每次用户自动登录时保持不变。它还包含一个随机令牌,该令牌在用户每次通过持久的“记住我”功能登录时都会重新生成。随机生成的序列和令牌的这种组合将持续存在,从而使暴力攻击非常不可能。

用例示例

public class LdapUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(final String userName) throws UsernameNotFoundException {
//Basically you need user details , username,password,roles you can fetch from your LdapService

        final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        final SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
        authorities.add(authority);

return new User(userName, "password", authorities);   }

Spring Security配置文件(spring-security.xml)

<!-- Remember me config -->
<security:http 
     <security:remember-me services-ref="rememberMeServices" />
</security:http>
<bean id="customUserDetailsService" class="com..LdapUserDetailsService"/>
<bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
    <constructor-arg index="0" value="myAppKey" />
    <constructor-arg index="1" ref="customUserDetailsService" />
 </bean>
 <bean id="rememberMeAuthenticationProvider" class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
    <constructor-arg index="0" value="myAppKey" />
  </bean>
  <security:authentication-manager alias="authenticationManager">
     <security:authentication-provider ref="rememberMeAuthenticationProvider"/>
  </security:authentication-manager>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在我的项目中正确配置Spring Security?

来自分类Dev

如何在我的项目中正确配置Spring Security?

来自分类Dev

Spring Security记住我

来自分类Dev

如何使用xml配置文件,JAVA,Spring Security使用LDAP对用户进行身份验证

来自分类Dev

使用Spring Security进行手动身份验证并记住我的提供程序

来自分类Dev

Spring Security LDAP和记住我

来自分类Dev

记住我Spring Security提供的功能

来自分类Dev

Spring Security记住我不工作

来自分类Dev

记住我Spring Security提供的功能

来自分类Dev

如何使用Spring Security 4 + Spring Rest API通过Angular JS + JWT身份验证开发“记住我”功能

来自分类Dev

如何使用spring Security通过基于邮件和uid的LDAP对用户进行身份验证?

来自分类Dev

如何在Spring Security中使用LDAP身份验证创建令牌

来自分类Dev

如何在Spring Security中使用LDAP身份验证创建令牌

来自分类Dev

如何在Android中添加“记住我”选项?

来自分类Dev

MVC身份|| 禁用所有cookie并“记住我”选项?

来自分类Dev

Spring Security记住我的cookie Unicode字符错误

来自分类Dev

Spring Security记住我的cookie Unicode字符错误

来自分类Dev

如何在Grails中使用Spring Security Rest插件进行身份验证

来自分类Dev

如何在Laravel 7中正确定义我的身份验证路由?

来自分类Dev

使用Spring Security配置自定义LDAP身份验证提供程序

来自分类Dev

如何在Spring Security中配置自定义身份验证过滤器-使用Java Config

来自分类Dev

如何在我的项目中正确加载 Python 类?

来自分类Dev

如何在Android中使用共享首选项创建带有记住我的登录表单

来自分类Dev

使用Spring Security + Spring数据+ MongoDB进行身份验证

来自分类Dev

使用Java配置的Spring Security预身份验证

来自分类Dev

使用身份验证回退配置Crowd Spring Security

来自分类Dev

如何在Spring Security中通过email_id或contact_number进行身份验证?

来自分类Dev

使用Gmail等Spring Security进行两因素身份验证

来自分类Dev

如何在Flyway中使用Spring Boot / Spring Security的JDBC身份验证

Related 相关文章

  1. 1

    如何在我的项目中正确配置Spring Security?

  2. 2

    如何在我的项目中正确配置Spring Security?

  3. 3

    Spring Security记住我

  4. 4

    如何使用xml配置文件,JAVA,Spring Security使用LDAP对用户进行身份验证

  5. 5

    使用Spring Security进行手动身份验证并记住我的提供程序

  6. 6

    Spring Security LDAP和记住我

  7. 7

    记住我Spring Security提供的功能

  8. 8

    Spring Security记住我不工作

  9. 9

    记住我Spring Security提供的功能

  10. 10

    如何使用Spring Security 4 + Spring Rest API通过Angular JS + JWT身份验证开发“记住我”功能

  11. 11

    如何使用spring Security通过基于邮件和uid的LDAP对用户进行身份验证?

  12. 12

    如何在Spring Security中使用LDAP身份验证创建令牌

  13. 13

    如何在Spring Security中使用LDAP身份验证创建令牌

  14. 14

    如何在Android中添加“记住我”选项?

  15. 15

    MVC身份|| 禁用所有cookie并“记住我”选项?

  16. 16

    Spring Security记住我的cookie Unicode字符错误

  17. 17

    Spring Security记住我的cookie Unicode字符错误

  18. 18

    如何在Grails中使用Spring Security Rest插件进行身份验证

  19. 19

    如何在Laravel 7中正确定义我的身份验证路由?

  20. 20

    使用Spring Security配置自定义LDAP身份验证提供程序

  21. 21

    如何在Spring Security中配置自定义身份验证过滤器-使用Java Config

  22. 22

    如何在我的项目中正确加载 Python 类?

  23. 23

    如何在Android中使用共享首选项创建带有记住我的登录表单

  24. 24

    使用Spring Security + Spring数据+ MongoDB进行身份验证

  25. 25

    使用Java配置的Spring Security预身份验证

  26. 26

    使用身份验证回退配置Crowd Spring Security

  27. 27

    如何在Spring Security中通过email_id或contact_number进行身份验证?

  28. 28

    使用Gmail等Spring Security进行两因素身份验证

  29. 29

    如何在Flyway中使用Spring Boot / Spring Security的JDBC身份验证

热门标签

归档