Shiro安全管理框架

shiro

shiro是一个安全管理的框架,可以验证发起本次请求的用户是否具有权限

shiro的使用

新建maven工程

在pom.xml中导入shiro的依赖
项目是基于spring和mybatis框架的,所以要事先将这两个框架的基础依赖导入

       <!-- shiro的依赖-->
       <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.6.0</version>
        </dependency>

在项目中加入web项目,在web.xml中配置一个代理过滤器

	<filter>
	<!--注意代理过滤器的命名-->
		<filter-name>shiro</filter-name>
		<!--这不是一个真正的过滤器,只是一个代理过滤器,代理过滤请求-->
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
        <filter-name>shiro</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--同时指明真正的代理类的位置-->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:application.xml, 
        classpath:application.xml
    </param-value>
</context-param>

在spring.xml中配置真是的代理类和安全管理器

<!--    开启扫描-->
    <context:component-scan base-package="Security"/>
    <!--给某个用户配置什么权限,什么角色,需要我们自己来决定
    配置登陆信息 -->
    <!--安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="loginRealm"/>
    </bean>
    <!--配置自定义规则bean-->
    <!--shiro的真实过滤器-->
    <bean id="shiro" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--loginUrl : 登陆页面的请求 重定向
        所以如果页面放到了WEB-INF下面,那么,这里写的就是controller的请求地址-->
        <property name="loginUrl" value="/user/tologin"/>
        <!--successUrl : 登陆成功之后, shiro帮我们跳转到哪个页面 /login
           shiro本身继承了登陆接口, 如果是复杂的登陆,我们最好还是自己写
        -->
        <!--unauthorizedUrl : 本次请求如果没有权限, 跳转到哪个提示页面-->
        <property name="unauthorizedUrl" value="/user/unauthorized"/>
        <!--securityManager : 安全管理器
            shiroFilter : shiro的过滤器只是拦截请求, securityManager 安全管理器来处理
        -->
        <property name="securityManager" ref="securityManager"/>

        <property name="filterChainDefinitionMap" ref="myMap"/>
        <!--<property name="filterChainDefinitions">
            &lt;!&ndash;
                anon : 无需任何全选就能访问
                authc : 表示必须登陆才能访问
                roles[角色] : 需要特定角色才能问
                perms: 需要这个权限才能访问
             这是四个过滤器 /user/toLogin=authc 这种写法,表示其中登陆验证过滤器来管理当前请求
             就是说,这个请求过来的时候, 由登陆验证过滤器来验证是否登陆, 其他过滤器放行
            </value>
        </property>-->
    </bean>

需要注意的点是
1. 安全管理是中的loginUrl是我们自己定义的规则类
2. 这里的shirofilter才是真是的过滤器,但是shirofilter也只是过滤请求,真正处理请求的是securityManager进行处理
3. 使用filterChainDefinitions设置过滤器过滤请求需要在标签中手动设置,为了方便,我们使用filterChainDefinitionMap标签,这里需要传入一个自定义的集合类设置集合内容,自定义的集合类需要实现FactoryBean接口,否则无法给filterChainDefinitionMap使用

@Component
public class MyMap implements FactoryBean<Map<String,String>> {
    @Autowired
    private PermsDao permsDao;
    @Override
    public Map<String,String> getObject() throws Exception {
        HashMap map=new HashMap<String,String>();
        List<Perms> perms = permsDao.selectAllPerms();
        for (Perms perm : perms){
            if("perms".equals(perm.getType())){
                map.put(perm.getUrl(),perm.getType().concat("[").concat(perm.getUrl()).concat("]"));
            }else {
                map.put(perm.getUrl(),perm.getType());
            }
        }
        return map;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

自定义规则类

自定义规则需要继承authorizingRealm,并重写doGetAuthenticationInfo和doGetAuthorizationInfo方法
doGetAuthenticationInfo可以进行登录认证,在这里可以将前台数据和数据库数据进行比较,如果比对成功,程序会继续执行,如果报错,则代表认证失败

@Component
public class LoginRealm extends AuthorizingRealm {
    //连接mybatis对数据进行查询比较
    @Autowired
    private UserDao userDao;
    @Autowired
    private User_PermsDao user_permsDao;
    //认证 : 登陆认证在这写
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
     UsernamePasswordToken userToken= (UsernamePasswordToken) authenticationToken;
     String userName=userToken.getUsername();
        User user = userDao.selectByName(userName);
        /*SimpleAuthenticationInfo需要传入三个参数
        第一个参数:传入一个实体类User
        第二个参数:用户密码
        第三个参数是域名
        **/
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
        return info;
    }

在登陆成功之后,会执行doGetAuthorizationInfo,对用户进行授权

 @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {		
    //获取域名中已经登录的User对象,并对User进行授权
        User user= (User) principalCollection.getPrimaryPrincipal();
        //list存放的是从数据库中查找到的设置好的权限设置
        List<String> list = user_permsDao.selectPermsByUid(user.getUid());
        Set<String> set = new HashSet<String>(list);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
         //根据集合中的权限对用户进行授权
        info.setStringPermissions(set);
        return info;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值