当我使用内存中的 xml 用户名/密码对其进行身份验证时,它可以工作,因此我的项目的下一个阶段是针对数据库进行身份验证。这是我提出持久性问题的地方。已经好几天了。
控制台中的输出告诉我它至少连接到我的数据库 b/c,这是我在 mysql 数据库中插入的用户名。
loadUserByUsername : ed23
但是即使用户名/密码正确,它也会返回登录失败页面,而不是进行身份验证。
安全xml
<global-method-security pre-post-annotations="enabled" />
<http auto-config="true" use-expressions="true" access-denied-page="/denied">
<intercept-url pattern='/main/home.jsp' access="hasRole('ROLE_USER')" />
<intercept-url pattern='/admin/admin.jsp' access="hasRole('ROLE_ADMIN')" />
<form-login login-page='/login.jsp' always-use-default-target="true" default-
target-url="/main/home" authentication-failure-url="/login_error.jsp?error=true"/>
<logout invalidate-session="true" logout-success-url='/login.jsp' />
</http>
<!-- authentication manager and password hashing -->
<authentication-manager alias="authenticationManager">
<authentication-provider ref="daoAuthenticationProvider"/>
</authentication-manager>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
<beans:property name="saltSource">
<beans:bean
class="org.springframework.security.authentication.dao.ReflectionSaltSource">
<beans:property name="userPropertyToUse" value="username"/>
</beans:bean>
</beans:property>
<beans:property name="passwordEncoder" ref="passwordEncoder"/>
</beans:bean>
<beans:bean id="userDetailsService"
class="com.project.professional.service.StaffServiceImpl">
</beans:bean>
<beans:bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
<beans:constructor-arg index="0" value="256"/>
</beans:bean>
登录jsp
<form action="${loginUrl}" method="post" name="loginForm">
<p>
<label for="j_username">Username</label>
<input id="j_username" name="j_username" type="text" />
</p>
<p>
<label for="j_password">Password</label>
<input id="j_password" name="j_password" type="password" />
</p>
<input type="submit
value="Login"/>
</form>
控制器
@RequestMapping(value = "/main/home", method = RequestMethod.GET)
public String getHomePage(Locale locale, Model model) {
return "/main/home";
}
@RequestMapping(value="/", method = RequestMethod.GET)
public String getLoginPage(ModelMap model) {
logger.info("This is the login page {}.");
return "login";
}
用户详情实现
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
System.out.println("loadUserByUsername : " + username);
//DAO<Staff> sessionFactory = null;
Session session = sessionFactory.getCurrentSession();
username = (username == null) ? "" : username;
Query query = session
.createQuery("from Staff where username=:username");
query.setParameter("username", username);
if (query.list().isEmpty()) {
return null;
}
return (UserDetails) query.list().get(0);
}
public Staff getStaffDetails(String username) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Staff where username=:username");
query.setParameter("username", username);
if (query.list().isEmpty()) {
return null;
}
return (Staff) query.list().get(0);
我可以从导航栏访问这些页面,但我无法保护它,因此只有在从登录页面进行身份验证时才能访问它们。我将不胜感激任何帮助。