在spring security使用方法保护
spring security可以对链接做权限检查,另外,可以对方法的调用做权限检查。比较简单易行的办法是,通过spring security提供的注解。比如:
@RequestMapping("/save.htm") @Secured("ROLE_ADMIN") public ModelAndView save(Product product, BindingResult result) { if (result.hasErrors()) { return new ModelAndView("input"); } this.productDao.saveOrUpdate(product); return new ModelAndView("redirect:/detail.htm", "id", product.getId()); }
这样调用保持product对象的save方法时会检查是否有ROLE_ADMIN角色。如果不符,将返回http 403。
该注解需要在spring配置文件中加入打开spring security注解的配置。另外,如果配置了却不能生效,比如用户不具备相应的角色也可以调用方法,一般是因为,该方法所属的bean和注解的配置不在同一个配置文件中。
比如上面的代码是Controller代码,放在xxx-servlet.xml文件中,那么该配置文件中应该设置:
<security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled" />
这篇文章上的评论的 RSS feed TrackBack URI