Java @@Autowired controller中的安全设置不起作用
在带有@Autowired的容器中使用@Secured注释无效Java @@Autowired controller中的安全设置不起作用,java,spring,dependency-injection,spring-security,annotations,Java,Spring,Dependency Injection,Spring Security,Annotations,在带有@Autowired的容器中使用@Secured注释无效 @Controller @RequestMapping("/") public class HelloController { @Autowired private ApplicationContext applicationContext; @ModelAttribute private String test() { // this applicationContext is
@Controller
@RequestMapping("/")
public class HelloController {
@Autowired
private ApplicationContext applicationContext;
@ModelAttribute
private String test() {
// this applicationContext is null
return "test";
}
@Secured("ROLE_USER")
@RequestMapping(value = "test", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
// this applicationContext is null
model.addAttribute("message", "Hello world!");
return "hello";
}
}
我认为问题在于,当使用@modeldattribute方法作为private时,在运行时将控制器创建为代理对象,在这种情况下,spring依赖项注入失败。访问属性applicationContext会导致NullPointerException
我从未见过将方法width@ModelAttribute注释创建为公共的建议(或规则)。没有@Secured annotation,代码运行正常。请告诉我问题出在哪里?私有字符串测试();公共字符串printWelcome(ModelMap模型)。如果您定义private,哪个方法会产生问题?如果您将模型属性方法定义为private,那么它就不应该工作,因为当spring使用反射框架时,由于private的原因,无法访问该方法。为了解决这个问题,我在示例中添加了注释。Inside test()方法或printWelcome()应用程序上下文为空。如果我删除@Secured注释applicationContext filled.@Eremite:表示如果删除@Secured,则applicationContext将为null(失败),这将对您有所帮助。