简体   繁体   中英

Spring security accessing principal

When using spring security, specifically with @notation; what is the proper way to access the principal in a Controller? Lets say the following is my controller, but I would like to access the principal in the secure() method somewhere...

@Controller
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(ModelMap map, @RequestParam(value="fail" , required=false) String fail){
        map.addAttribute("title", "Login: AD Credentials");
        if(fail != null){
            map.addAttribute("error", "Invalid credentials");
        }
        return("login");
    }

    @RequestMapping("/secure")
    @PreAuthorize("isAuthenticated()")
    public String secure(ModelMap map, String principal){
        System.out.println(principal);
        return("secure");
    }


}

The easiest is SecurityContextHolder.getContext().getAuthentication().getPrincipal() . Works via thread-local pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM