I am trying to get spring-boot-starter-security
to work along with spring-boot-starter-web
and spring-boot-starter-tomcat
. I tried following the guide from spring-boot-sample-secure and spring-boot-sample-web-secure however I did not get it to work.
I am trying to build a REST application without any ui interactions. Hence I found both samples are not fully suitable for my purpose. Currently my solution is by using AOP.
@Before("execution(* my.zin.rashidi.openshift.tomcat.controller.*.*(..)) && args(authorization, ..)")
public void authenticate(String authorization) {
if (!isEmpty(authorization)) {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken("user", "N/A",
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"))
);
}
}
@RequestMapping(method = GET)
public ResponseEntity<User> get(@RequestHeader String authorization, @RequestBody User user) {
HttpStatus status = OK;
User returnObject = null;
try {
returnObject = service.get(user);
} catch (AuthenticationCredentialsNotFoundException e) {
status = UNAUTHORIZED;
}
return new ResponseEntity<User>(returnObject, status);
}
The solutions is working for me. However I would like to know if this is a good solution. I'm curious if there is a better solution.
Thanks in advanced for your helps!