Skip to content

Commit 7bdb8ec

Browse files
author
eugenp
committed
finished work on the user access post
1 parent aef5e03 commit 7bdb8ec

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

spring-security-rest-custom/src/main/java/org/baeldung/security/CustomAuthenticationProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.security.core.AuthenticationException;
1010
import org.springframework.security.core.GrantedAuthority;
1111
import org.springframework.security.core.authority.SimpleGrantedAuthority;
12+
import org.springframework.security.core.userdetails.User;
13+
import org.springframework.security.core.userdetails.UserDetails;
1214
import org.springframework.stereotype.Component;
1315

1416
@Component
@@ -27,7 +29,8 @@ public Authentication authenticate(final Authentication authentication) throws A
2729
if (name.equals("admin") && password.equals("system")) {
2830
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
2931
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
30-
final Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
32+
final UserDetails principal = new User(name, password, grantedAuths);
33+
final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
3134
return auth;
3235
} else {
3336
return null;

spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.baeldung.web.controller;
22

33
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.core.userdetails.UserDetails;
45
import org.springframework.stereotype.Controller;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RequestMethod;
@@ -18,6 +19,8 @@ public SecurityController3() {
1819
@RequestMapping(value = "/username3", method = RequestMethod.GET)
1920
@ResponseBody
2021
public String currentUserNameSimple(final Authentication authentication) {
22+
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
23+
System.out.println("Retrieved user with authorities: " + userDetails.getAuthorities());
2124
return authentication.getName();
2225
}
2326

0 commit comments

Comments
 (0)