Simulate login from spring web mvc controller

code as below:



List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
grantedAuthorities.add(new GrantedAuthorityImpl("USER"));

UsernamePasswordAuthenticationToken uat = new UsernamePasswordAuthenticationToken(
					"john", "admin", grantedAuthorities);

 uat.setDetails(user);
 SecurityContext context = SecurityContextHolder.getContext();

 Authentication userAuth = authenticationManager.authenticate(uat);

context.setAuthentication(userAuth);

你可能感兴趣的:(controller)