【发布时间】:2018-09-28 01:01:53
【问题描述】:
我使用 Spring Security 进行身份验证。因为我想访问用户对象,所以我将这个身份验证保存在 @SessionAttribut 中。
建议方法
@ControllerAdvice(basePackages= {"com.sencerseven.blog.admin","com.sencerseven.blog.controller"})
@SessionAttributes("User")
public class SessionScope {
@Autowired
private UserService userService;
@ModelAttribute("User")
public User session() {
Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
if(authentication != null) {
User user = userService.getByEmail(authentication.getName());
if(user != null) {
return user;
}
}
return null;
}
}
控制器
@Controller
@RequestMapping("/post")
public class PostController {
@RequestMapping(value= {"/{post}"})
public ModelAndView postPage(@SessionAttribute("User")User tempUser,@PathVariable("post")String tempPostName ) throws NotFoundException {
...
.....
}
@PostMapping("addcomment")
@ResponseBody
public String saveComment(@SessionAttribute("User")User tempUser,@RequestParam("post_id")int id,@RequestParam("comment")String tempComment) {
....
.....
}
}
@SessionAttribute("User") 在 PostPage 方法中返回 null,页面抛出异常
java.lang.NullPointerException at com.sencerseven.blog.controller.PostController.postPage(PostController.jav a:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
.但是SaveComment方法是正确的。SaveComment访问用户对象中的@SessionAttribute("User")它不是null返回。
为什么?我该如何解决这个问题?
【问题讨论】:
-
由于 session() 在身份验证失败时返回 null,也许它就在这里。尝试返回默认用户或打印日志来验证这一点。
-
使用不在它周围的框架。 Spring Security 允许您简单地注入
Authentication对象或您自己的用户,您不需要自己将其添加到会话中。请参阅 docs.spring.io/spring-security/site/docs/current/reference/html/… 并使用它而不是解决它。 -
另外,正如我在您的另一个(现已删除?)问题中提到的那样,
@SessionAttributes和@SessionAttribute是不同的东西,不应该这样使用。跨度>
标签: java spring session spring-security