安全框架:shiro

shiro的概念

1.是java的安全框架,权限框架
2.shiro的四大基石
身份验证(Authentication):登录
授权(Authorization):验证是否有权限,没有权限就不能访问,有权限就能访问
​ 密码(Cryptography):加密加盐
​ 会话管理(Session Management):shiro 自己有套会话机制,使用方式类似javaweb session机制
在这里插入图片描述
3.类似还有spring Security框架,但比较shiro更细致,配置比较麻烦,是重量级框架。而shiro是Apache公司的轻量级框架。

shiro与spring整合

导包

<dependencies>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

配置applicationContext-shiro.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
">
    <!--配置安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"></property>
    </bean>
    <!--配置realm-->
    <bean id="myRealm" class="cn.itsource.aisell.shiro.MyRealm">
        <property name="credentialsMatcher" >
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="MD5"></property>
                <property name="hashIterations" value="10"></property>
            </bean>
        </property>
    </bean>



    <!-- 3.lifecycleBeanPostProcessor:可以自动调用在Spring IoC窗口中 Shiro bean的生成周期方法 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
    <!-- 4.启动ioc容器中使用 shiro的注解,但是必需配置在Spring Ioc容器中Shiro bean的生成周期方法 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!-- 真实的过滤器处理 id这个名字 必须和web.xml里面配置的代理的过滤器的名称一样,才行-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

        <property name="securityManager" ref="securityManager"></property>
        <!-- 如果没有认证,会跳转该页面-->
        <property name="loginUrl" value="/s/login.jsp"></property>
        <!--认证成功,跳转的页面-->
        <property name="successUrl" value="/s/main.jsp"></property>
        <!--没有权限-->
        <property name="unauthorizedUrl" value="/s/unauthorized.jsp"></property>
        <!-- 拦截配置-->
       <!-- <property name="filterChainDefinitions">
            <value>
                /s/login.jsp = anon
                /login = anon
                /s/permission.jsp=perms[user:*]
                /** =  authc
            </value>
        </property>-->
        <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap1"></property>

    </bean>
    <!-- 配置两个bean-->
    <bean id="filterChainDefinitionMap1" factory-bean="filterMapFactoyBean" factory-method="createFilterChainDefinitionMap">
    </bean>
    <bean id="filterMapFactoyBean" class="cn.itsource.aisell.shiro.AisellFilterChainDefinitionMap"></bean>
    
</beans>

注意在web.xml中配置过滤器

 <!-- 过滤器filter 代理的过滤器,在spring配置文件里面去找一个shiro的真实的过滤器去处理 -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <!-- 配置参数 如果为true 就会找真实过滤器 -->
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

shiro的身份认证流程(登录流程)

步骤
a)前台传入用户名和密码到controller
b)得到主体
SecurityUtils.getSubject()
c)判断主体是否认证过
没有认证 --调用login方法去认证–调用realm的认证代码
如果已经认证–跳转到主页面

登录认证里面 加密加颜
流程:我们告诉shiro 我们使用MD5加密 和加密次数,以及盐值,我们shiro
它会自动把页面的密码进行加密加盐在和数据库密码进行比较

@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //认证方法
        //UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //相当于用户名 lcq 123456 admin 123456
        Object principal = token.getPrincipal();
        //数据库查询密码
        Object credentials = "831d092d59f6e305ebcfa77e05135eac";
        //得到认证对象
        ByteSource salt = ByteSource.Util.bytes("itsource");
        //返回的时候,会自动进行比对密码
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(principal,credentials,salt,getName());
        return authenticationInfo;
    }

    /*public static void main(String[] args) {
        SimpleHash simpleHash = new SimpleHash("MD5","123456","itsource",10);
        System.out.println(simpleHash.toString());
        //输出831d092d59f6e305ebcfa77e05135eac
    }*/

shiro的授权流程

如果要测试授权,必须先登录
当我认证通过之后,如果要去访问一个需要授权的页面(permission.jsp)

–判断代码是shiro这个框架自己做
shiro这个框架,就会判断当前该用户是否具备相应的权限
如果有访问权限–让你访问
如果没有访问的权限–跳转未授权的页面

代码写到realm里面 doGetAuthorizationInfo

@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //授权方法
        String username =(String) principalCollection.getPrimaryPrincipal();

        //根据用户名得到权限代码
        Set<String> permissions = getPermissionsByUsername(username);

        //shiro就会自己取进行权限的比较
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    public Set<String> getPermissionsByUsername(String username){
        Set<String> permissions = new HashSet<String>();
        if("cq".equals(username)){

            permissions.add("user:*");
        }
        return permissions;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值