我有一个小webapp,使用sql-db上的用户名/密码组合作为凭证,由spring-security保护。
我现在想用spring-social添加facebook/twitter身份验证。使用这些示例,我能够将用户凭证存储在我的数据库中。我现在正在使用以下代码在我的应用程序上根据用户的当前会话对用户进行身份验证:
public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
User user = userService.getUserById(Long.parseLong(userId));
user.setPassword(this.passwordEncoder.encodePassword(user.getAccessToken(), this.salt));
this.userService.store(user);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getDisplayName(), user.getAccessToken());
HttpServletRequest req = request.getNativeRequest(HttpServletRequest.class); // generate session if one doesn't exist
token.setDetails(new WebAuthenticationDetails(req));
Authentication authenticatedUser = this.authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
return "/user/dashboard";
}
身份验证起作用了,我没有得到任何BadCredential异常。但是在被重定向到/user/dashboard之后,我又回到了登录位置。
我想不到了,在一个经典的注册之后,一个类似的验证会话的代码正在工作。
有谁知道为什么会发生这种情况,或者如何调试?
非常感谢提前!亨德里克
发布于 2012-03-22 14:31:42
我有类似的代码为我工作,还增加了“记住我”的支持:
// lookup by id, which in my case is the login
User user = userService.findByLogin(userId);
// code to populate the user's roles
List<GrantedAuthority> authorities = ...;
// create new instance of my UserDetails implementation
UserDetailsImpl springSecurityUser = new UserDetailsImpl(user, authorities);
// create new Authentication using UserDetails instance, password, and roles
Authentication authentication = new UsernamePasswordAuthenticationToken(springSecurityUser, user.getPassword(), authorities);
// set the Authentication in the SecurityContext
SecurityContextHolder.getContext().setAuthentication(authentication);
// optional: remember-me support (must @Autowire in TokenBasedRememberMeServices)
tokenBasedRememberMeServices.onLoginSuccess(
(HttpServletRequest) request.getNativeRequest(),
(HttpServletResponse) request.getNativeResponse(),
authentication);https://stackoverflow.com/questions/9301491
复制相似问题