diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/security/AuthenticationFacade.java b/spring-security-rest-custom/src/main/java/org/baeldung/security/AuthenticationFacade.java new file mode 100644 index 0000000000..5a3c77b070 --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/security/AuthenticationFacade.java @@ -0,0 +1,21 @@ +package org.baeldung.security; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; + +@Component +public class AuthenticationFacade implements IAuthenticationFacade { + + public AuthenticationFacade() { + super(); + } + + // API + + @Override + public final Authentication getAuthentication() { + return SecurityContextHolder.getContext().getAuthentication(); + } + +} diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/security/IAuthenticationFacade.java b/spring-security-rest-custom/src/main/java/org/baeldung/security/IAuthenticationFacade.java new file mode 100644 index 0000000000..d1223e41cf --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/security/IAuthenticationFacade.java @@ -0,0 +1,9 @@ +package org.baeldung.security; + +import org.springframework.security.core.Authentication; + +public interface IAuthenticationFacade { + + Authentication getAuthentication(); + +} \ No newline at end of file diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java index d6eeea5f77..ff5a00fd95 100644 --- a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java @@ -1,7 +1,5 @@ package org.baeldung.web.controller; -import javax.servlet.http.HttpServletResponse; - import org.baeldung.web.dto.Foo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; @@ -10,7 +8,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.util.UriComponentsBuilder; @Controller @RequestMapping(value = "/foo") @@ -27,7 +24,7 @@ public class FooController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody - public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { + public Foo findOne(@PathVariable("id") final Long id) { return new Foo(); } diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController1.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController1.java new file mode 100644 index 0000000000..d06f15af93 --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController1.java @@ -0,0 +1,39 @@ +package org.baeldung.web.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SecurityController1 { + + @Autowired + private ApplicationEventPublisher eventPublisher; + + public SecurityController1() { + super(); + } + + // API + + @RequestMapping(value = "/username1", method = RequestMethod.GET) + @ResponseBody + public String currentUserName() { + final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (!(authentication instanceof AnonymousAuthenticationToken)) { + final String currentPrincipalName = authentication.getName(); + System.out.println("Authentication: " + authentication); + System.out.println("Principal: " + authentication.getPrincipal()); + return currentPrincipalName; + } + + return null; + } + +} diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController2.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController2.java new file mode 100644 index 0000000000..ac02738d0c --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController2.java @@ -0,0 +1,25 @@ +package org.baeldung.web.controller; + +import java.security.Principal; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SecurityController2 { + + public SecurityController2() { + super(); + } + + // API + + @RequestMapping(value = "/username2", method = RequestMethod.GET) + @ResponseBody + public String currentUserNameSimple(final Principal principal) { + return principal.getName(); + } + +} 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 new file mode 100644 index 0000000000..a4f2e80469 --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController3.java @@ -0,0 +1,24 @@ +package org.baeldung.web.controller; + +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SecurityController3 { + + public SecurityController3() { + super(); + } + + // API + + @RequestMapping(value = "/username3", method = RequestMethod.GET) + @ResponseBody + public String currentUserNameSimple(final Authentication authentication) { + return authentication.getName(); + } + +} diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController4.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController4.java new file mode 100644 index 0000000000..0434cc5fdc --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController4.java @@ -0,0 +1,28 @@ +package org.baeldung.web.controller; + +import java.security.Principal; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SecurityController4 { + + public SecurityController4() { + super(); + } + + // API + + @RequestMapping(value = "/username4", method = RequestMethod.GET) + @ResponseBody + public String currentUserNameSimple(final HttpServletRequest request) { + final Principal principal = request.getUserPrincipal(); + return principal.getName(); + } + +} diff --git a/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController5.java b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController5.java new file mode 100644 index 0000000000..e1a0c35cc6 --- /dev/null +++ b/spring-security-rest-custom/src/main/java/org/baeldung/web/controller/SecurityController5.java @@ -0,0 +1,30 @@ +package org.baeldung.web.controller; + +import org.baeldung.security.IAuthenticationFacade; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SecurityController5 { + + @Autowired + private IAuthenticationFacade authenticationFacade; + + public SecurityController5() { + super(); + } + + // API + + @RequestMapping(value = "/username5", method = RequestMethod.GET) + @ResponseBody + public String currentUserNameSimple() { + final Authentication authentication = authenticationFacade.getAuthentication(); + return authentication.getName(); + } + +}