From 7bdb8ec66dca8e3589d329db7dfbd790b1443bdd Mon Sep 17 00:00:00 2001 From: eugenp Date: Wed, 17 Jul 2013 23:17:36 +0300 Subject: [PATCH] finished work on the user access post --- .../org/baeldung/security/CustomAuthenticationProvider.java | 5 ++++- .../org/baeldung/web/controller/SecurityController3.java | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/security/CustomAuthenticationProvider.java b/spring-security-rest-custom/src/main/java/org/baeldung/security/CustomAuthenticationProvider.java index e2ea9842af..4b9b3e8ee9 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/security/CustomAuthenticationProvider.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/security/CustomAuthenticationProvider.java @@ -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 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; diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java index a4f2e80469..fb207b43dd 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java @@ -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(); }