apache-shiro奇怪的东西通过spring-boot集成到spring-mvc中

时间:2015-07-15 02:00:55

标签: spring spring-mvc spring-boot shiro

我想使用apache-shiro来保护我的网址。 所以,我做了这个配置

@Configuration
public class ApplicationConfigShiro {

    @Bean
    public ShiroFilterFactoryBean shiroFilter() {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager());
        return factoryBean;
    }

    // blah blah ...

    @Bean(name = "realm")
    @DependsOn("lifecycleBeanPostProcessor")
    public PropertiesRealm realm() {
        // blah blah
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        return new DefaultAdvisorAutoProxyCreator();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager());
        return advisor;
    }
}

这是我的mvc-request-method

@Controller
@RequestMapping("/html")
public class HtmlController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public ModelAndView listUsers() {
        List<User> users = userService.findAllUsers();
        ModelAndView modelAndView = new ModelAndView("users");
        modelAndView.addObject("users", users);
        return modelAndView;
    }

}

非常可以。我按mvn spring-boot:run开始我的项目。并查看http://localhost:8080/mappings json结果可以告诉我映射/html/users已准备好使用。 我确实看了http://localhost:8080/html/users,没关系。

但是一旦我将shiro的注释(例如@RequiresAuthentication)添加到我的请求映射方法中,就像这样:

@RequiresAuthentication    // **I ADD THIS LINE**
@RequestMapping(value = "/users", method = RequestMethod.GET)
public ModelAndView listUsers() {
    List<User> users = userService.findAllUsers();
    ModelAndView modelAndView = new ModelAndView("users");
    modelAndView.addObject("users", users);
    return modelAndView;
}

重新运行此项目,重新检查http://localhost:8080/mappings,映射/html/users只是GONE。尝试访问http://localhost:8080/html/users我当然得到了404。

我错过了什么吗?或者这是春天的错误?

请帮帮我。很抱歉给你一个麻烦。

git clone git@github.com:yingzhuo/spring-playground.git
cd spring-playground
mvn clean spring-boot:run

1 个答案:

答案 0 :(得分:1)

好的,我找到了myslef的答案。

@Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true); // this SETTING
        return proxyCreator;
    }