Danubian Sailor Danubian Sailor - 1 year ago 192
Java Question

How to get logged user name/Principal in Spring MVC REST channel?

I have Spring MVC REST channel:

public class REST {

and I have my method:

@RequestMapping(value = "/doSomething")
public @ResponseBody DoSomethingResultDTO doSomething(
@RequestBody DoSomethingRequestDTO)

Now I need the name of the user that is logged in. Normally I could do it by the method


but how to get it here? I have annotations for headers (
), or even cookies (
). But how can I get the
in my method?

Answer Source

You can inject Principal object to your controller handler method

@RequestMapping(value = "/doSomething")
public @ResponseBody DoSomethingResultDTO doSomething(
    @RequestBody DoSomethingRequestDTO, Principal principal)

See the spring reference manual for more info

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download