如何在Spring Security身份验证登录中让用户输入用户名和密码的值
我在应用程序中使用Spring MVC,并且登录由Spring
Security验证。我的UserServiceImpl.java
课程中有以下两种方法,公共UserDetails
loadUserByUsername(String
userName)引发UsernameNotFoundException,DataAccessException {
ApplicationTO applicationTO = null; try
{
applicationTO = applicationService.getApplicationTO(adminDomainName);
}
catch (ApplicationPropertyException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
UserTO userTO = getUserTO(applicationTO.getApplicationId(), userName);
if (userTO == null)
{
throw new UsernameNotFoundException("user not found");
}
httpSession.setAttribute("userTO", userTO);
return buildUserFromUserEntity(userTO);
}
User buildUserFromUserEntity(UserTO userTO)
{
String username = userTO.getUsername();
String password = userTO.getPassword();
int userId = userTO.getUserId();
int applicationId = userTO.getApplicationId();
boolean enabled = userTO.isEnabled();
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthority(applicationId, userId));
return user;
}
我对Spring比较陌生,对spring安全性部分也不了解。在我的spring-security.xml
文件中,我有以下内容,
<form-login login-page="/login" default-target-url="/module/user-home/welcome" authentication-failure-url="/login?error" username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
我的登录表单的操作设置如下:
<form id="loginForm" class="form-horizontal" role="form" name='loginForm' action="${rc.getContextPath()}/j_spring_security_check" method='POST'>
现在,我试图获取password
用户在登录表单中输入的值,或者在loadUserByUsername
方法内部,或者通过向UserServiceImpl.java
类添加新方法。
因此,在登录期间,springsecurity将用户输入的密码与数据库中的加密密码进行了比较,登录失败。但是根据上面链接中建议的实现,有一种方法可以比较密码和加密的密码,以检查它们是否相同。仅当我可以访问用户输入的密码时,才有可能。这就是我试图获取用户输入密码的原因。
回答:
正如MangEngkus的回答所建议的那样,您可以实现自己的自定义
但是根据您的描述,我认为您不需要这样做。
您不需要在spring-
security中实现自己的密码哈希机制。您只需要从spring本身定义BCryptPasswordEncoder。
可以通过以下两种方式使用默认方式:
<authentication-manager> <authentication-provider>
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
或创建自己的bean并将其提供给默认提供程序:
<authentication-manager> <authentication-provider>
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="15" />
</beans:bean>
但是对您来说,这就是方法::)
<beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> <beans:property name="userDetailsService" ref="userDetailsService"/>
<beans:property name="passwordEncoder" ref="encoder" />
</beans:bean>
<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
以上是 如何在Spring Security身份验证登录中让用户输入用户名和密码的值 的全部内容, 来源链接: utcz.com/qa/431133.html