Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing spring user in service layer and controllers - any update for Spring 3.2?

I want to access the current logged in user I am doing it like this (from a static method)

public static User getCurrentUser() {

final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (principal instanceof User) {
  return (User) principal;
  }
}

or injecting and casting like this :

@RequestMapping(value = "/Foo/{id}", method = RequestMethod.GET)
public ModelAndView getFoo(@PathVariable Long id, Principal principal) {
        User user = (User) ((Authentication) principal).getPrincipal();
..

Where user implements userdetails, both seem a bit lame is there a better way in Spring 3.2 ?

like image 747
NimChimpsky Avatar asked Oct 06 '22 08:10

NimChimpsky


1 Answers

I don't think that it has something new in spring 3.2 for that purpose. Have you thought about using a custom annotation?

Something like this :

The controller with the custom annotation :

@Controller
public class FooController {

    @RequestMapping(value="/foo/bar", method=RequestMethod.GET)
    public String fooAction(@LoggedUser User user) {
        System.out.print.println(user.getName());
        return "foo";
    }
}

The LoggedUser annotation :

@Target(ElementType.PARAMETER)
@Retention(RententionPolicy.RUNTIME)
@Documented
public @interface LoggedUser {}

The WebArgumentResolver :

public class LoggedUserWebArgumentResolver implements WebArgumentResolver {

    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) {
        Annotation[] annotations = methodParameter.getParameterAnnotations();

        if (methodParameter.getParameterType().equals(User.class)) {
            for (Annotation annotation : annotations) {
                if (LoggedUser.class.isInstance(annotation)) {
                    Principal principal = webRequest.getUserPrincipal();
                    return (User)((Authentication) principal).getPrincipal();
                }
            }
        }
        return WebArgumentResolver.UNRESOLVED;
    }
}

Beans configuration :

<bean id="loggedUserResolver" class="com.package.LoggedUserWebArgumentResolver" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="customArgumentResolver" ref="loggedUserResolver" />
</bean>
like image 181
Jean-Philippe Bond Avatar answered Oct 10 '22 03:10

Jean-Philippe Bond