finished work on the user access post
This commit is contained in:
parent
aef5e03097
commit
7bdb8ec66d
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue