public String addRoleToUser(Integer[] ids, Integer userId){ //从后台获取到当前认证通过后的用户名 SecurityContextHolder.getContext...().getAuthentication().getName(); String username = ((SysUser) SecurityContextHolder.getContext...().getAuthentication().getPrincipal()).getUsername(); userService.addRoleToUser(userId, ids);
RequestParam("file") MultipartFile myFile) throws IOException { WebGoatUser user = (WebGoatUser) SecurityContextHolder.getContext...().getAuthentication().getPrincipal(); File destinationDir = new File(fileLocation, user.getUsername...RequestParam("file") MultipartFile myFile) throws IOException { WebGoatUser user = (WebGoatUser) SecurityContextHolder.getContext...().getAuthentication().getPrincipal(); File destinationDir = new File(fileLocation, user.getUsername
().getAuthentication().getPrincipal(); log.info("当前登录的用户信息:{}", user.toString()); return...().getAuthentication().getPrincipal(); log.info("当前登录的用户信息:{}", user.toString()); return...().getAuthentication().getPrincipal(); log.info("当前登录的用户信息:{}", user.toString()); return...(value = "/secured") @Secured("ROLE_USER") public User hello() { User user = (User) SecurityContextHolder.getContext...().getAuthentication().getPrincipal(); log.info("当前登录的用户信息:{}", user.toString()); return
().setAuthentication(authResult); 这样一行简单的保存代码,至少带给我们两方面的信息: 以后在任何地方,只要想获取用户信息,只需要调用 SecurityContextHolder.getContext...().getAuthentication() 就可以获取到。...以后在任何地方,只要想更新用户信息,只要调用 SecurityContextHolder.getContext().setAuthentication(authResult); 就可以更新。...1.获取用户登录信息 用户信息的获取,其实办法很多,上面提到的 SecurityContextHolder.getContext().getAuthentication() 是一个静态方法,也就是利用这个方法..."/hr/info") public Hr getCurrentHr(Authentication authentication) { return ((Hr) authentication.getPrincipal
() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); return...= null && auth.getPrincipal() !...((UserDetails)auth.getPrincipal()).getUsername() : auth.getPrincipal().toString(); } else {...= null && auth.getPrincipal() != null ?...= null && auth.getPrincipal() != null) { Collection<?
controller中,直接参数注入 @RequestMapping public void execute(Principal principal){} 其他bean中 SecurityContextHolder.getContext...().getAuthentication().getPrincipal()
RequestMethod.GET) public String homePage(ModelMap model) { model.addAttribute("user", getPrincipal...logoutPage (HttpServletRequest request, HttpServletResponse response) { Authentication auth = SecurityContextHolder.getContext...().getAuthentication(); if (auth !...logout"; } private String getPrincipal(){ String userName = null; Object principal...= SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof
().getAuthentication() == null){ SecurityContextHolder.getContext().setAuthentication...HttpServletRequest) servletRequest)); } }else{ Authentication auth = SecurityContextHolder.getContext...().getAuthentication(); System.out.println(auth == null); if(auth !...instanceof UsernamePasswordAuthenticationToken){ if(principal.toString().equals(auth.getPrincipal...().toString())){ SecurityContextHolder.getContext().setAuthentication(null);
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal
().getAuthentication(); //通用替换,针对用户名密码登录的 if(authentication instanceof UsernamePasswordAuthenticationToken...UsernamePasswordAuthenticationToken token=new UsernamePasswordAuthenticationToken(originToken.getPrincipal...(),"N/A",originToken.getAuthorities()); token.setDetails(person); SecurityContextHolder.getContext...userAuthentication; String username = (String) originalUsernamePasswordAuthentication.getPrincipal...usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(originalUsernamePasswordAuthentication.getPrincipal
."); } Authentication principal = SecurityContextHolder.getContext().getAuthentication(); if (principal...parameter.getParameter(), RegisteredOAuth2AuthorizedClient.class); // 获取当前会话上下文认证信息 Authentication principal = SecurityContextHolder.getContext...().getAuthentication(); // 优先使用注解中的客户端注册ID if (!...authorizedClient = authorizeRequest.getAuthorizedClient(); Authentication principal = authorizeRequest.getPrincipal
SecurityContext 无论是有状态的Session模式还是流行的JWT模式你都可以通过SecurityContext来获取当前的用户: Authentication authentication = SecurityContextHolder.getContext...().getAuthentication(); String currentPrincipalName = authentication.getName(); 当然这种方式是不够严谨的,如果接口允许匿名访问很可能返回一个匿名用户...,而匿名用户并不能直接通过getName获取,所以我们需要优化上面的逻辑为: Authentication authentication = SecurityContextHolder.getContext...().getAuthentication(); if (!...); } 当然你还可以通过expression参数声明SpEL表达式来获取其它属性,例如获取Principal对象: @GetMapping("/principal") public String getPrincipal
System.out.println("3.登陆成功信息"); //获取数据,转成LoginUser对象 LoginUser loginUser = (LoginUser) authenticate.getPrincipal...); } @GetMapping("/info") public User info() { Authentication authentication = SecurityContextHolder.getContext...().getAuthentication(); LoginUser loginUser = (LoginUser) authentication.getPrincipal();...} @GetMapping("/exit") public String exit() { Authentication authentication = SecurityContextHolder.getContext...().getAuthentication(); LoginUser loginUser = (LoginUser) authentication.getPrincipal();
如果请求头中有 token 并且 token 的格式正确,则进行解析并判断 token 的有效性,然后会在 Spring Security 全局设置授权信息SecurityContextHolder.getContext...那么,既然这样,我们在其他地方获取到当前登录用户的授权信息也就很简单了,通过SecurityContextHolder.getContext().getAuthentication();方法即可。...().getAuthentication(); if (authentication !...= null && authentication.getPrincipal() !...= null) { return (String) authentication.getPrincipal(); } return null;
() { 13 14 Object obj = SecurityContextHolder.getContext().getAuthentication() 15....getPrincipal(); 16 17 if (obj instanceof UserDetails) { 18 return ((UserDetails...getPrincipal().equals("anonymous"); 26 } 27 28 public static boolean allGranted(String[] checkForAuths...true; 54 } 55 56 private static Set getUserAuthorities() { 57 Object obj = SecurityContextHolder.getContext...().getAuthentication() 58 .getPrincipal(); 59 Set roles = new HashSet
this.eventPublisher.publishEvent(new AuthenticationSwitchUserEvent( SecurityContextHolder.getContext...().getAuthentication(), targetUser)); } return targetUserRequest; } 从url读取username...().getAuthentication(); } GrantedAuthority switchAuthority = new SwitchUserGrantedAuthority...().getAuthentication(); if (null == current) { throw new AuthenticationCredentialsNotFoundException...get the source user details UserDetails originalUser = null; Object obj = original.getPrincipal
或者通过request.getSession().getAttribute("SPRING_SECURITY_CONTEXT")也可以获取User对象 SecurityContext context = SecurityContextHolder.getContext...();// 从上下文中获取当前登陆的用户 User user = (User) context.getAuthentication().getPrincipal(); String username =...request.getRemoteAddr(); // 获取当前操作的用户 SecurityContext context = SecurityContextHolder.getContext...();// 从上下文中获取当前登陆的用户 User user = (User) context.getAuthentication().getPrincipal(
; result.put("status", "200"); result.put("用户信息", (User) authentication.getPrincipal...; result.put("status", "200"); result.put("用户信息", (User) authentication.getPrincipal...().getAuthentication() !...().getAuthentication() + "'")); this.securityContextRepository.saveContext(context, request,...().getAuthentication(), this.getClass())); } if (this.successHandler !
= "principal") @ResponseBody public Object principal() { SecurityContext context = SecurityContextHolder.getContext...(); Authentication authentication = context.getAuthentication(); Object principal = authentication.getPrincipal
password ); Authentication authenticate = authenticationManager.authenticate(token); SecurityContextHolder.getContext...().setAuthentication(authenticate); UserInfo userInfo = (UserInfo) authenticate.getPrincipal(...(){ SecurityContext context = SecurityContextHolder.getContext(); return context.getAuthentication...=null){ Object principal = authentication.getPrincipal(); if(principal!...=null){ UserInfo userInfo = (UserInfo) authentication.getPrincipal();