【发布时间】:2015-08-04 12:52:34
【问题描述】:
我尝试使用 Spring MVC 使用用户名和密码手动登录
我有 CustomSessionAuthenticationProvider:
public class CustomSessionAuthenticationStrategy implements AuthenticationProvider {
@Autowired
private UserService userService;
@Override
public Authentication authenticate(Authentication authentication) {
String username = authentication.getName();
String password = (String) authentication.getCredentials();
User user = userService.loadUserByUsername(username);
if (user == null) {
throw new BadCredentialsException("Username not found.");
}
if (!password.equals(user.getPassword())) {
throw new BadCredentialsException("Wrong password.");
}
Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
@Override
public boolean supports(Class<?> arg0) {
return true;
}
}
我将它包含在安全配置中:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
我不明白如何手动验证,如果我有控制器用户名和密码并从中获取 sessionId 和令牌。
方法验证输入需要验证,但我需要登录名和密码。
【问题讨论】:
标签: spring spring-mvc spring-security