無法使Spring Security
與數據庫身份驗證提供程序一起使用。
內存認證提供程序正常工作。Spring Security:數據庫身份驗證提供程序
步驟重現:
當我登錄憑證與sb
,sb
,的AuthenticationService
login()
方法返回false
。
Tomcat
沒有相關的日誌。
的applicationContext.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/chirokDB?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
服務層:
@Service("authenticationService")
public class AuthenticationServiceImpl implements AuthenticationService {
@Resource(name = "authenticationManager")
private AuthenticationManager authenticationManager;
public boolean login(String username, String password) {
try {
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
username, password));
if (authenticate.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticate);
return true;
}
} catch (AuthenticationException e) {
}
return false;
}
託管bean級別:
public String doLogin() {
boolean isLoggedIn = authenticationService.login(name, password);
if (isLoggedIn) {
return "index";
}
FacesContext.getCurrentInstance().addMessage("login failure", new FacesMessage());
return "failureLogin";
}
的applicationContext-security.xml文件:
<global-method-security pre-post-annotations="enabled"/>
<http auto-config="true">
<form-login login-page="/login.xhtml" default-target-url="/index.xhtml"/>
<intercept-url pattern="/contacts.xhtml" access="ROLE_ANONYMOUS,ROLE_USER"/>
<intercept-url pattern="/delivery.xhtml" access="ROLE_USER"/>
<logout invalidate-session="true"/>
<session-management>
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</session-management>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"/>
</authentication-provider>
</authentication-manager>
持續電平:
MySql DB
具有以下標準表(由Spring必需):
1.用戶
2.當局
users
表有記錄,用戶名='sb'和密碼='sb'
authorities
表有用戶名記錄= 'SB' 和權威= 'ROLE_USER'
注
與用戶內存中所有可與以下配置OK:
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="sb" password="sb" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
假設:
dataSource
注入org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl
至於Hibernate使用的ORM,或許應該使用JdbcDaoImpl
以外的其他應用程序?
從何種意義上說它不起作用? – jtoberon
jtoberon,我已更新我的帖子。請參閱「步驟重現」部分。 – sergionni
什麼是AuthenticationService? – jtoberon