java - Spring Security使用数据库进行身份验证

标签 java spring spring-security

我正在努力使用数据库进行 Spring 安全身份验证。只是说它不起作用 - 我无法登录我的用户,它总是将我重定向到 accessdenied。

应用程序安全.xml

<http auto-config="true"  use-expressions="true">
        <csrf disabled="true"/>
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />
        <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
        <form-login login-page="/login" default-target-url="/AddUser.html" authentication-failure-url="/accessdenied" />
        <logout logout-url="/j_spring_security_logout" logout-success-url="/logout"/>
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
               users-by-username-query="select username,password,enabled from user_authentication where username=?"
               authorities-by-username-query="select u1.username, u2.role from user_authentication u1, user_authorization u2 where u1.user_id = u2.user_id and u1.username =?" />
        </authentication-provider>
    </authentication-manager>

我的用户=“abcd”,密码为“abcd”。数据库查询 select u1.username, u2.role from user_authentication u1, user_authorization u2 where u1.user_id = u2.user_id and u1.username ='abc' 返回 'abcd' 和 'ROLE_ADMIN'。我的登录表单一定没问题(当我在 application-security.xml 中使用硬编码的用户名和密码时,一切都很好)。数据源也很好 - 它适用于 CRUD 操作。有什么想法可能是错误的吗?

最佳答案

在表单登录中:

<form-login login-page="/login" 
    default-target-url="/AddUser.html" 
    authentication-failure-url="/accessdenied" />

您没有任何参数,例如:

username-parameter="username"
password-parameter="password"

所以只需添加它们:

<form-login login-page="/login" 
        default-target-url="/AddUser.html" 
        username-parameter="username"
        password-parameter="password"
        authentication-failure-url="/accessdenied" />

关于java - Spring Security使用数据库进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744552/

相关文章:

java - 在 Controller 方法中的方法上使用 @PreAuthorize 注释

java - 没有 web.xml 的 Spring 安全自定义身份验证过滤器

java - 将 X509 值传递到 LDAP 服务器

javascript - 加载应用程序中通过 Web 服务器公开的 Assets 文件夹中托管的图像文件

java - 在 Java 中打印特殊字符,如 FS 和 ESC。像 Notepad++

java - 双重拆分一个字符串并将其添加到 JTable

java - 如何在 Google Code Jam 中使用 .in 文件作为 eclipse 的输入?

Spring Boot SSL 客户端

java - Spring 和 Struts2 REST - junit 测试

java - Tapestry5 使用带有构造函数参数的注入(inject)服务