天天看點

五、Shrio內建Spring

一、POM依賴

  注意和Spring和SpringMVC的版本相容

<dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>      

二、Spring配置檔案需要添加的東西(Spring就是對所有的bean管理,而注解什麼的由SpringMVC配置檔案管理,是以關于Shiro的元素都需要放至Spring配置檔案中)

<?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">
    <!-- Spring配置檔案 -->

    <!--Shiro過濾器-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--設定Shiro的securityManager-->
        <property name="securityManager" ref="securityManager"/>
        <!--登入網址-->
        <property name="loginUrl" value="login.html"/>
        <!--未授權網址-->
        <property name="unauthorizedUrl" value="403.html"/>
        <!--過濾器鍊 anon匿名通路的路徑  authc必須驗證通過才能通路的路徑 配置在前面的優先生效!-->
        <property name="filterChainDefinitions">
            <value>
                /login.html = anon
                /sublogin = anon
                /* = authc
            </value>
        </property>
    </bean>

    <!--配置securityManager,注意在Spring中使用的是DefaultWebSecurityManager,在非web環境下,使用DefaultSecurityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--配置資料源-->
        <property name="realm" ref="realm"/>
    </bean>

    <!--配置資料源-->
    <bean id="realm" class="org.pc.util.CustomRealm">
        <!--配置加密對象-->
        <property name="credentialsMatcher" ref="matcher"/>
    </bean>

    <!--配置加密對象-->
    <bean id="matcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <!--加密算法-->
        <property name="hashAlgorithmName" value="md5"/>
        <!--加密次數-->
        <property name="hashIterations" value="1"/>
    </bean>
</beans>      
<!--配置shiroFilter過濾器-->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>      
@Controller
public class LoginController {
    /**
     * produces = "application/json;charset=utf-8":解決傳回中文亂碼問題
     * 注意:以上解決辦法僅限于SpringMVC架構傳回json資料出錯的問題,如果加入jackson對json處理,就不會出現亂碼問題
     */
    @PostMapping(value = "/sublogin", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(User user){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }
        return "登陸成功";
    }
}