springboot1.5 shiro配置

import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 *
 * @author Administrator
 * @create 2017/12/2
 * @since 1.0.0
 */
@Configuration
public class ShiroConfiguration  {

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager){
        System.out.println("shiroFilter");
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        //配置shiro默认登录界面地址,前后端分离中登录界面跳转应由前端路由控制,后台仅返回json数据
        bean.setLoginUrl("/login/unauth");
        // 登录成功后要跳转的链接
        //bean.setSuccessUrl("/user/index");
        //未授权界面
        //bean.setUnauthorizedUrl("/user/unauthorized");

        //请求拦截 key-访问请路径  value-使用的拦截方式
        LinkedHashMap<String, String> filterChainDefinittionMap = new LinkedHashMap<>();
        //配置退出
        filterChainDefinittionMap.put("/login/logout", "logout");
        // 静态资源
        filterChainDefinittionMap.put("/WEB-INF/views/**", "anon");
        //filterChainDefinittionMap.put("/index","authc");//需要通过验证
        filterChainDefinittionMap.put("/login/loginUser","anon");//不需要验证

       // filterChainDefinittionMap.put("/user/admin","roles[管理员]");//只允许admin用户
        //filterChainDefinittionMap.put("/add","perms[add]");//只允许权限又add的用户
        //filterChainDefinittionMap.put("/**","user");//需要验证--已经登录的用户
        //filterChainDefinittionMap.put("/**","authc");
        bean.setFilterChainDefinitionMap(filterChainDefinittionMap);
        return bean;
    }
    //注入缓存管理器
    @Bean("securityManager")
    public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm){
        System.out.println("securityManager");
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(authRealm);
        return manager;
    }

    @Bean("authRealm")
    public AuthRealm authRealm(@Qualifier("credentialMatcher") CredentialMatcher matcher){
        System.out.println("authRealm");
        AuthRealm authRealm = new AuthRealm();
        authRealm.setCacheManager(new MemoryConstrainedCacheManager());//启用缓存
        authRealm.setCredentialsMatcher(matcher);
        return  authRealm;
    }

    @Bean("credentialMatcher")
    public CredentialMatcher credentialMatcher(){
        System.out.println("Credentialmatcher");
        return new CredentialMatcher();
    }
    //开启shiro aop注解支持
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager){
        System.out.println("authorizationAttributeSourceAdvisor");
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        System.out.println("defaultAdvisorAutoProxyCreator");
        DefaultAdvisorAutoProxyCreator creater = new DefaultAdvisorAutoProxyCreator();
        creater.setProxyTargetClass(true);
        return creater;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值