shiro:集成Spring(四)

基于【加密及密码比对器(三)】项目改造

引入相关依赖环境

shiro-spring已经包含 shiro-core和shiro-web 所以这两个依赖可以删掉

<!--shiro继承spring依赖包-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.0</version>
</dependency>

构建shiro配置文件

替代shiro.ini文件
resources\spring-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">

    <!--注册自定义realm-->
    <bean id="myRealm" class="com.shiro.realm.MyRealm">
        <!--把UserService注册到realm-->
        <property name="userService" ref="userServiceImpl"></property>
        <!--声明密码比对器-->
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="SHA-256" />
                <property name="storedCredentialsHexEncoded" value="false" />
                <property name="hashIterations" value="10000" />
            </bean>
        </property>
    </bean>

    <!--声明securityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>

    <!--shiroFilter 角色权限校验-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--注入核心对象:securityManager-->
        <property name="securityManager" ref="securityManager" />
        <!--未登录跳转路径-->
        <property name="loginUrl" value="/user/login" />
        <!--没权限的跳转路径-->
        <property name="unauthorizedUrl" value="/user/error" />
        <property name="filterChainDefinitions">
            <value>
                /user/login = anon
                /getuser = anon
                /getrole = anon
                /user/delUser = authc,roles["admin","manager"]
                /user/getallUsers = authc
                /user/logout = logout
            </value>
        </property>
    </bean>

</beans>

把userserivce接口注入到remla中

com\shiro\realm\MyRealm.java

private UserService userService;

public void setUserService(UserService userService) {
    this.userService = userService;
}

在doGetAuthorizationInfo和doGetAuthenticationInfo中直接用userService调用相关方法

把shiro配置文件引入到applicationContext.xml

resources\applicationContext.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">

    <import resource="spring-mapper.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="springmvc-servlet.xml"/>
    <import resource="spring-shiro.xml"/>
</beans>

修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--注册DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--启动级别-1-->
        <load-on-startup>5</load-on-startup>
    </servlet>

    <!--/ 匹配所有的请求;(不包括.jsp)-->
    <!--/* 匹配所有的请求;(包括.jsp)-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--注册shiroFilter
       shiroFilter要与spring-shiro.xml中的一致
       -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <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>
    
</web-app>
posted @ 2020-04-24 22:32  努力的校长  阅读(148)  评论(0编辑  收藏  举报