SpringBoot學習筆記(4)

weixin_33749242發表於2017-09-05

開啟專案的resources目錄,新建 shiro-spring.xml 檔案,新增以下內容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"
       xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
       xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)">

    <!--安全管理器配置-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--系統身份認證Realm(賬號密碼和許可權校驗)-->
        <property name="realm" ref="systemAuthorizingRealm" />
    </bean>

    <!--安全認證過濾器-->
    <bean class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--配置安全管理器-->
        <property name="securityManager" ref="securityManager"/>
        <!--配置登入頁面,不設定預設為Web工程下的login.jsp頁面-->
        <property name="loginUrl" value="/login"/>
        <!--設定授權成功的頁面-->
        <property name="successUrl" value="/manage"/>
        <!--設定未授權的頁面-->
        <property name="unauthorizedUrl" value="/"/>

        <!--配置攔截器 anon不需要認證 authc需要認證-->
        <property name="filterChainDefinitions">
            <value>
                /login.action = anon
                /css/** = anon
                /dist/** = anon
                /fonts/** = anon
                /img/** = anon
                /js/** = anon
                / = anon
                /logout = logout
                /** = authc
            </value>
        </property>
    </bean>

    <!--AOP方法級許可權檢查-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true"/>
    </bean>

    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- 基於配置檔案式的異常處理 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.apache.shiro.authz.UnauthorizedException">index</prop>
                <prop key="com.sirdc.modules.core.exceptions.ServiceException">index</prop>
            </props>
        </property>
    </bean>

    <!-- 基於註解式子的異常處理 -->
    <bean class="com.lanshiqin.cmsboot.core.exception.MyExceptionResolver"/>
    <!-- Shiro end -->

</beans>

新建XMLConfig類

package com.lanshiqin.cmsboot.core.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 匯入工程的xml配置檔案
 */
@Configuration
@ImportResource("classpath:shiro-spring.xml")
public class XmlConfig {
}


相關文章