Java Spring Security:SecurityContextHolder.getContext().getAuthentication()在用户注册用户并重定向到登录后页面后返回null

Java Spring Security:SecurityContextHolder.getContext().getAuthentication()在用户注册用户并重定向到登录后页面后返回null,java,spring,spring-mvc,spring-security,security-context,Java,Spring,Spring Mvc,Spring Security,Security Context,成功注册用户后,我尝试将用户重定向到登录后页面,并尝试从SecurityContextHolder.getContext().getAuthentication()获取活动用户;返回null 下面是当用户成功注册时我如何设置上下文 UsernamePasswordAuthenticationToken authenticatedUser = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetail

成功注册用户后,我尝试将用户重定向到登录后页面,并尝试从SecurityContextHolder.getContext().getAuthentication()获取活动用户;返回null

下面是当用户成功注册时我如何设置上下文

 UsernamePasswordAuthenticationToken authenticatedUser = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
 authenticatedUser.setDetails(userDetails);

 if(authenticatedUser.isAuthenticated()) {
        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
 }
然后我使用response.sendRedirect(url)将用户重定向到x页面

在控制器方法中,我尝试将当前用户作为

@RequestMapping("/<pattern>")
public @ResponseBody <method_name>(HttpServletRequest req,HttpServletResponse resp){
     Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth==null){
            System.out.println("this is null");
        }
        else{
          // do something
        }
}
@RequestMapping(“/”)
public@ResponseBody(HttpServletRequest-req,HttpServletResponse-resp){
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
if(auth==null){
System.out.println(“这是空的”);
}
否则{
//做点什么
}
}
我看到控制台中的文本为“this is null”

如何从SecurityContext()获取并保留当前用户/主体,直到用户注销为止。

我得到了解决方案

问题在于该特定URI组的标记的模式属性

早些时候它是/path/*并且失败了。我把它改成了/path/**并且成功了

如果有人能解释一下这两种模式匹配(/*和/**)之间的关系,那会很有帮助。

我找到了解决方案

问题在于该特定URI组的标记的模式属性

早些时候它是/path/*并且失败了。我把它改成了/path/**并且成功了


如果有人能够解释这两种模式匹配(/*和/**)之间的关系,这将非常有用。

检查以获取当前记录器用户名。获取所有详细信息
User User=SecurityContextHolder.getContext().getAuthentication().getPrincipal()@user2264997这是一个打字错误。我现在编辑了它。请检查以获取当前记录器用户名。获取所有详细信息
User User=SecurityContextHolder.getContext().getAuthentication().getPrincipal()@user2264997这是一个打字错误。我现在编辑了它。请检查以获取当前记录器用户名。获取所有详细信息
User User=SecurityContextHolder.getContext().getAuthentication().getPrincipal()@user2264997这是一个打字错误。我现在编辑了它。http标记的模式属性http标记的模式属性http标记的模式属性