finished work on the user access post

This commit is contained in:
eugenp 2013-07-17 23:17:36 +03:00
parent aef5e03097
commit 7bdb8ec66d
2 changed files with 7 additions and 1 deletions

View File

@ -9,6 +9,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
@Component
@ -27,7 +29,8 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
if (name.equals("admin") && password.equals("system")) {
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
final Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
final UserDetails principal = new User(name, password, grantedAuths);
final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
return auth;
} else {
return null;

View File

@ -1,6 +1,7 @@
package org.baeldung.web.controller;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -18,6 +19,8 @@ public class SecurityController3 {
@RequestMapping(value = "/username3", method = RequestMethod.GET)
@ResponseBody
public String currentUserNameSimple(final Authentication authentication) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
System.out.println("Retrieved user with authorities: " + userDetails.getAuthorities());
return authentication.getName();
}