在我的Spring项目中,我定义了自己的自定义身份验证提供程序。在引入Spring Security之前,我在Java代码中使用了BCrypt,现在在数据库中进行BCrypting后保存密码。如何将brcypt编码器引用到定制身份验证提供程序?
弹簧security.xml文件
<security:authentication-manager>
<security:authentication-provider ref="myAuthenticationProvider">
</security:authentication-provider>
</security:authentication-manager>
<b:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
<b:bean id="myAuthenticationProvider" class="com.cT.www.provider.CustomAuthenticationProvider">
</b:bean>
而且我自定义的身份验证提供如下所示。
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
public CustomAuthenticationProvider() {
super();
}
@Autowired
private PersonService personService;
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
System.out.println(authentication.getName() + "principal" +(String) authentication.getCredentials());
String username = authentication.getName();
String password = (String) authentication.getCredentials();
UserSignUp user = (UserSignUp) personService.loadUserByUsername(username);
if (user == null || !user.getUsername().equalsIgnoreCase(username)) {
throw new BadCredentialsException("Username not found.");
}
if (!password.equals(user.getPassword())) {
throw new BadCredentialsException("Wrong password.");
}
List<Role> authorities = user.getAuthorities();
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
@Override
public boolean supports(Class<?> arg0) {
// TODO Auto-generated method stub
return true;
}
}
我不想使用user-service-ref
在弹簧security.xml文件wihtin认证经理。
我得到这个org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:authentication-provider元素在与'ref'属性一起使用时不能有子元素。另外我想引用我的自定义身份验证提供程序。 – user3705478