我正在開發一個帶有struts2,hibernate和spring的應用程序。 我正在嘗試整合spring安全性。 這是我springSecurity.xml:使用數據庫中的數據整合彈簧安全
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/welcome"
access="permitAll" />
<security:form-login login-page="/login/formLogin"
default-target-url="/login/welcome"
authentication-failure-url="/login/formLogin?login_error=1"
username-parameter="username"
password-parameter="password" />
<security:logout />
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="datasource"
users-by-username-query=
"select id, Rol_id, email, motDePasse from user u where u.email=?"
authorities-by-username-query=
"select u.email,r.role from user u, role r where u.Rol_id=r.id and u.email=?"
/>
</security:authentication-provider>
</security:authentication-manager>
表用戶和角色:
CREATE TABLE IF NOT EXISTS `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role` varchar(254) DEFAULT NULL,
`description` varchar(254) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Rol_id` int(11) NOT NULL,
`email` varchar(254) DEFAULT NULL,
`motDePasse` varchar(254) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_Association_user_role` (`Rol_id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
的login.jsp
<form name='loginForm'
action="${pageContext.request.contextPath}/j_spring_security_check"
method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
的問題如下:
即使我提供正確的用戶名和密碼,我收到「Bad credentials」。 任何幫助表示讚賞!
你的查詢是錯誤的,'用戶s-by-username-query'必須返回3個代表'username','password'和'enabled'的字段,它們應該分別是'String','String'和'boolean'。然而,你返回更多的領域,顯然第三不是'boolean'。 –
首先,感謝回覆, 是否意味着我的表格必須尊重這個模式? – user3604388
對不起,但我是一個初學者與彈簧安全 – user3604388