我正在努力使用数据库进行 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/