Spring-Security:升级到 Spring-Security 4.1 后,登录时用户名发送为空

2023-11-22

我正在开发一个基于 Spring-MVC 的应用程序,其中我们使用 Spring-Security 进行登录、授权。我们之前使用的是3.2.5版本,升级为4.1.1。但升级后,登录机制被破坏,因为发送的用户名为空。我不知道出了什么问题。你能帮忙的话,我会很高兴。

安全应用程序Context.xml:

<security:http pattern="/resources/**" security="none"/>

    <security:http create-session="ifRequired" use-expressions="true" auto-config="false" disable-url-rewriting="true">
        <security:form-login login-page="/login" login-processing-url="/j_spring_security_check" default-target-url="/dashboard" always-use-default-target="false" authentication-failure-url="/denied" />
        <!--<security:remember-me key="_spring_security_remember_me" user-service-ref="userDetailsService" token-validity-seconds="1209600" data-source-ref="dataSource"/>-->
        <security:logout delete-cookies="JSESSIONID" invalidate-session="true" logout-url="/j_spring_security_logout"/>
<!--     <security:intercept-url pattern="/**" requires-channel="https"/> -->
        <security:intercept-url pattern="/j_spring_security_check" access="permitAll" />
        <security:port-mappings>
        <security:port-mapping http="8080" https="8443"/>
    </security:port-mappings>
    <security:logout logout-url="/logout" logout-success-url="/" success-handler-ref="myLogoutHandler"/>

     <security:session-management session-fixation-protection="migrateSession">
         <security:concurrency-control session-registry-ref="sessionRegistry" max-sessions="5" expired-url="/login"/>
     </security:session-management>
    <security:csrf disabled="true"/>
    </security:http>
 <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="LoginServiceImpl">
           <security:password-encoder  ref="encoder"/>
        </security:authentication-provider>
    </security:authentication-manager>

    <beans:bean id="encoder"
                class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <beans:constructor-arg name="strength" value="11" />
    </beans:bean>

    <beans:bean id="daoAuthenticationProvider"
                class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
                <beans:property name="userDetailsService" ref="LoginServiceImpl"/>
               <beans:property name="passwordEncoder" ref="encoder"/>
    </beans:bean>

网络.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/appServlet/security-applicationContext.xml</param-value>
    </context-param>

    <session-config>
        <session-timeout>2440</session-timeout>
    </session-config>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

调试日志:

DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/j_spring_security_check'; against '/resources/**'
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 1 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession returned null object for SPRING_SECURITY_CONTEXT
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@56af71ce. A new one will be created.
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 2 of 12 in additional filter chain; firing Filter: 'ConcurrentSessionFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 3 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 4 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
DEBUG: org.springframework.security.web.header.writers.HstsHeaderWriter - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@2a868e9a
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 5 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/j_spring_security_check'; against '/j_spring_security_logout'
DEBUG: org.springframework.security.web.FilterChainProxy - /j_spring_security_check at position 6 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/j_spring_security_check'; against '/j_spring_security_check'
DEBUG: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Request is to process authentication
DEBUG: org.springframework.security.authentication.ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
Username is 
DEBUG: org.springframework.security.authentication.dao.DaoAuthenticationProvider - User '' not found
DEBUG: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials
DEBUG: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Updated SecurityContextHolder to contain null Authentication
DEBUG: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Delegating to authentication failure handler org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler@75d32cb4
DEBUG: org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler - Redirecting to /denied
DEBUG: org.springframework.security.web.DefaultRedirectStrategy - Redirecting to '/denied'

登录代码:

<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-4 col-lg-offset-4">
    <div class="container-fluid white-div">
        <div class="col-xs-12 col-md-12">
            <h2><spring:message code="login.title"/></h2>
            <form id="login-form" class="login-page" action="<c:url value='/j_spring_security_check'/>" method="POST">
                <div class="form-group">
                    <label><spring:message code="login.label.email"/> <span id="eMailError" class="red-font"></span></label>
                    <input type="email" name="j_username" id="j_username" value="" class="form-control input" placeholder="<spring:message code="common.input.email.placeholder"/>">
                </div>
                <div class="form-group">
                    <label><spring:message code="login.label.password"/> <span id="passwordError" class="red-font"></span></label>
                    <input type="password" name="j_password" id="j_password"  class="form-control input"  placeholder="<spring:message code="login.password.placeholder"/>">
                </div>
                <div class="checkbox">
                    <a href="#" class="forgotpassword pull-right" data-toggle="modal" data-target="#passwordForgotModal"><spring:message code="login.forgotpassword"/></a>
                </div>
                <br>
                <div class="form-group">
                    <button class="btn btn-primary btn-lg pull-right"><spring:message code="login.submit"/></button>
                </div>
                <div class="form-group">
                    <span><spring:message code="login.registration.text"/> <a href="/registration" class="forgotpassword"><spring:message code="login.registration.link"/></a></span>
                </div>
            </form>
        </div>
    </div>

你能帮忙的话,我会很高兴。多谢。


问题可能是从 Spring Security 版本 4.+ 开始,表单登录的参数名称更改了名称:

  • username代替j_username
  • password代替j_password
  • POST to /login网址而不是/j_spring_security_check

因此,您应该重命名登录表单中的参数,或者使用表单登录参数覆盖这些参数名称,如下所示:

<form-login login-page="/login"
                    username-parameter="j_username" 
                    password-parameter="j_password" 
                    login-processing-url="/j_spring_security_check" 
                    ..../>

请看一下这个资源,它可能对您的迁移有帮助 -http://docs.spring.io/spring-security/site/migrate/current/3-to-4/html5/migrate-3-to-4-xml.html#m3to4-xmlnamespace-form-login

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-Security:升级到 Spring-Security 4.1 后,登录时用户名发送为空 的相关文章

随机推荐

  • Cypress:具有三个不同响应的同一路由的存根响应

    我的应用程序中有一个端点 我们为每个请求使用相同的 api 并在参数中使用不同的操作 URL application api 示例请求负载 1 action CARD TRANSACTION HISTORY data date from 2
  • python中什么时候需要使用global关键字

    好吧 所以我遇到了这个非常烦人的问题 其中一个变量在本地设置 但随后在该函数之外恢复到它的旧值 在本例中为 无 但同时我可以操纵其他变量 并且仍然可以不使用 全局 关键字 我无法提供真正的代码 但它是这样的 foo foo foo bar
  • 如何在SQL Server中预编译存储过程?

    有没有办法在 SQL Server 中预编译存储过程 我的要求是这样的 我有一些存储过程 编译时间比执行时间多 所以我想预编译所有存储过程 当数据库服务器启动并运行时预编译它们会很好 任何关于这方面的想法都会非常有帮助 使用 SET FMT
  • WebView 不调整大小

    我已经为 Firefox OS 开发了一个 Web 应用程序 但我想让它在 Android 中 本地 运行 我的应用程序由一个对话框组成div它在可见时填充整个页面 我使用 Firefox 响应式设计工具对其进行了测试 并且它确实可以正确调
  • SQLite 支持引用完整性吗?

    我有一个 Django Web 服务器配置为使用 SQLite 在多对多关系 带有额外字段 中 Django 强制我使用关系模型来建立两个对象之间的关系 但我能够在相关表中尚不存在的对象之间创建关系 例如 I have table1 and
  • Django 模板如何使用变量查找字典值

    mydict key1 value1 key2 value2 在 Django 模板中查找字典值的常规方法是 mydict key1 mydict key2 如果键是循环变量怎么办 IE for item in list where ite
  • jquery:选择文本事件

    当用户选择一些文本 非文本区域或输入 时 jquery 是否可以调用我的回调来让我知道选择了哪个 div 的文本 并且如果选择焦点丢失也调用我的回调 Thanks 有点令人惊讶的是 没有简单的方法可以做到这一点 IE 有一个select在所
  • 提交表单中的 Laravel 日期格式 (d-M-Y)

    在刀片模板的提交表单中 我有以下日期表单 它与默认日期一起正常工作 例如Y m d 但我想显示日期d M Y 我试图找到一个可用的解决方案 但运气不佳 以下是适用于默认日期的代码 这是模型 public static rules birth
  • R根据条件创建因子

    我需要根据数值高于或低于 10 将列从数字更改为因子 例如 有以下数据 age lt c 1 20 hight lt c 1 20 d frame lt data frame age hight 我尝试了以下方法 d frame hight
  • Android GridView 带分隔符

    GridView 的元素之间可以有分隔符吗 Thanks 您可能需要使用填充和背景颜色 将表的背景设置为一种颜色 并将表中每个视图的背景设置为另一种颜色 在表中的每个视图周围设置 1 或 2 像素的填充 并且之间应该有边框
  • 如何在 Apple 的时钟应用程序中实现 Apple 的“贪睡”功能?

    我正在深入研究 iOS 开发 并正在构建自己的闹钟应用程序以熟悉该平台和 SDK 我目前正在学习的 API 之一是本地通知 API 我认为它与 Apple 用于在时钟应用程序中实现闹钟的 API 相同 我不明白的是他们如何实现 贪睡 功能
  • Heroku 与 Postgres 上的 ConnectionTimeoutError

    我在 Heroku 上部署的应用程序遇到问题 它在本地环境上运行良好 但部署到 Heroku 时经常会出现应用程序错误 日志中的异常是 ActiveRecord ConnectionTimeoutError 无法在 5 000 秒内获取数据
  • WCF、ASP.NET 成员资格提供程序和身份验证服务

    我编写了一个与 WCF 服务 BasicHttpBinding 通信的 Silverlight 2 应用程序 托管 Silverlight 内容的站点使用 ASP NET 成员资格提供程序进行保护 我可以使用 WCF 服务中的 HttpCo
  • 删除App时的委托方法

    当应用程序即将被删除时 iOS 中是否有委托方法 我正在尝试向服务器发送设备将不再接收通知的信息 不会 删除应用程序时不会触发委托方法
  • DeflatorInputStream 和 DeflatorOutputStream 不重建原始数据

    我想压缩一些数据 所以我遇到了 DeflatorInputStream 和 DeflatorOutputStream 类 但是 下面的示例表明 在使用这些类时我似乎无法重建原始数据 当我切换到 ZipInputStream 和 ZipOut
  • Java 比较数组

    我有两个未知类型的数组 有没有办法检查元素是否相同 public static boolean equals Object a Object b if a instanceof int return Arrays equals int a
  • python 可以读取电子表格中单元格的值吗?

    All python 可以读取电子表格中单元格的值吗 从绘图 GIS 分析的角度来看 最简单的示例是在给定 shapefile GIS 数据集 上运行缓冲区 邻近 工具的脚本 对于缓冲区距离参数 脚本将指向电子表格单元格中的值 首选 lib
  • 如何在我的应用程序中显示谷歌语音识别设置?

    我正在开发一个 Android 应用程序 其中实现了语音识别和 TTS 因此 我正在考虑启动 Google 语音识别和 TTS 的设置屏幕 以允许用户从应用程序内更改设置 我已经使用以下代码成功实现了 TTS 设置 intent new I
  • 按优先级从 MSMQ 获取消息

    我通过设置优先级在 MSMQ 中发送消息 使用 C 我可以先从 MSMQ 获取具有高优先级的消息吗 就像我们进入优先队列一样 还有一件事 假设有三个优先级 0 高 1 中等 2 低 队列中的序列是 2001122221111100 现在 如
  • Spring-Security:升级到 Spring-Security 4.1 后,登录时用户名发送为空

    我正在开发一个基于 Spring MVC 的应用程序 其中我们使用 Spring Security 进行登录 授权 我们之前使用的是3 2 5版本 升级为4 1 1 但升级后 登录机制被破坏 因为发送的用户名为空 我不知道出了什么问题 你能