Spring集成Shiro

簡(jiǎn)介

Spring是一個(gè)開(kāi)源框架,最早由Rod Johnson創(chuàng)建,Spring是為了解決企業(yè)級(jí)應(yīng)用開(kāi)發(fā)的復(fù)雜性而創(chuàng)建的,使用Spring可以讓簡(jiǎn)單的JavaBean實(shí)現(xiàn)之前只有EJB才能完成的事情。但Spring不僅僅局限于服務(wù)器端開(kāi)發(fā),任何Java應(yīng)用都能在簡(jiǎn)單性,可測(cè)試性和松耦合等方面從Spring獲益。

Shiro是一個(gè)簡(jiǎn)單易用的Java安全框架,Shiro對(duì)JavaBean的兼容性使得Shiro適合Sping的XML配置機(jī)制,在web開(kāi)發(fā)中常常在Spring MVC中集成Shiro,加固我們的web應(yīng)用。

本文主要演示通過(guò)Idea創(chuàng)建Spring集成Shiro項(xiàng)目的過(guò)程,并對(duì)其中的原理進(jìn)行分析。

創(chuàng)建應(yīng)用

創(chuàng)建與配置項(xiàng)目

1、首先通過(guò)Idea創(chuàng)建一個(gè)maven項(xiàng)目。然后配置其Facts為Spring項(xiàng)目,Web 應(yīng)用。
操作為:點(diǎn)擊項(xiàng)目Structure。添加Spring Facets,Spring MVC Facets,Web 。

圖1、配置項(xiàng)目結(jié)構(gòu)的位置
圖2、配置項(xiàng)目的Facets

2、添加項(xiàng)目的依賴(lài)
修改pom.xml,在文件中添加如下依賴(lài):

    <dependencies>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

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

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

3、 然后配置項(xiàng)目的Tomcat服務(wù)器。參考使用Idea管理Tomcat,在web應(yīng)用中不要忘了添加依賴(lài)的Maven庫(kù)。

圖3、配置服務(wù)器

增加必要的文件

在Resources目錄下新建Spring配置文件,名字分別為applicationContext.xml,Sping-mvc.xml。
1、Spring-mvc文件內(nèi)容如下:

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

    <!--在classpath路徑下掃描注解的Bean-->
    <context:component-scan base-package="me.aihe" />
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

2、ApplicationContext.xml 文件內(nèi)容如下:

<?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="UserRealm" class="me.aihe.UserRealm" />
    <!--里面可以有額外的屬性-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="UserRealm"></property>
    </bean>
    <!--必須要有這樣一個(gè)實(shí)例,管理shiro中常見(jiàn)對(duì)象-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
        <property name="arguments" ref="securityManager"/>
    </bean>


    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="successUrl" value="/success.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        <property name="filterChainDefinitions">
            <value>
                /login.jsp  = anon
                /login = anon
                /logout = logout
                /** = authc
            </value>
        </property>
    </bean>
</beans>

3、web.xml內(nèi)容

<?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_3_1.xsd"
         version="3.1">

    <display-name>Spring Shiro Tutorial</display-name>

    <!-- Spring 配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spingg MVC 配置 -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--Shiro配置-->
    <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>

添加Realm類(lèi)文件

Realm是shiro獲取安全數(shù)據(jù)的地方,從這里獲取用戶(hù)信息。

package me.aihe;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm extends AuthorizingRealm {
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 從數(shù)據(jù)庫(kù)獲取認(rèn)證信息
      //一般token為用戶(hù)輸入的表單,此次我們演示簡(jiǎn)單的daemon,忽略。

        String user = "aihe";
        String password = "aihe";

        //交給AuthenticatingRealm使用CredentialsMatcher進(jìn)行密碼匹配,如果覺(jué)得人家的不好可以自定義實(shí)現(xiàn)
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user, //用戶(hù)名
                password, //密碼
                null,//不加鹽
                getName()  //realm name
        );

        return authenticationInfo;


    }
}

添加登錄控制器

package me.aihe;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by aihe on 2017/6/15.
 */
@Controller
public class LoginCtrl {

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest req , HttpSession session){

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Subject subject = SecurityUtils.getSubject();

        try {

            subject.login(new UsernamePasswordToken(username, password));
            if (subject.isAuthenticated()) {
                String principal = (String) subject.getPrincipal();

                session.setAttribute("username", principal);

                return "redirect:/success";
            }

        } catch (Exception e) {
            req.setAttribute("shiroLoginFailure", e.getClass().getName());
        }

        return "redirect:login.jsp";
    }

    @RequestMapping("/success")
    public String sucess(){
        return "success";
    }
}

編寫(xiě)基本視圖頁(yè)面

1、Login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登錄</title>
</head>
<body>
<h1>登錄界面</h1>
<form role="form" action="${pageContext.request.contextPath}/login" method="post">
        <div class="form-group">
            <input class="form-control" placeholder="賬戶(hù)名" name="username" autofocus>
        </div>
        <div class="form-group">
            <input class="form-control" placeholder="密碼" name="password" type="password">
        </div>
        <!-- Change this to a button or input when using this as a form -->
        <input type="submit" value="登錄" class="btn btn-primary form-control">
</form>

</body>
</html>

2、登錄成功界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功</title>
</head>
<body>
<h1>內(nèi)容頁(yè)面</h1>
<a href="${pageContext.request.contextPath}/logout">   退出當(dāng)前用戶(hù) </a>
</body>
</html>

項(xiàng)目結(jié)構(gòu)

最后整個(gè)項(xiàng)目的結(jié)構(gòu)如圖

圖4、項(xiàng)目最終結(jié)構(gòu)圖

其它的頁(yè)面,可以自己隨便寫(xiě)。

測(cè)試

1、程序啟動(dòng)時(shí),直接進(jìn)入登錄頁(yè)面

圖5、程序啟動(dòng)界面

2、登錄失敗,登錄失敗之后,程序會(huì)重定向到登錄頁(yè)面


圖6、登錄失敗

3、訪(fǎng)問(wèn)其它頁(yè)面 沒(méi)有登錄的時(shí)候,訪(fǎng)問(wèn)其它頁(yè)面,也會(huì)重定向到login.jsp

圖7、訪(fǎng)問(wèn)其它頁(yè)面重定向

4、登錄成功,登錄成功的時(shí)候,進(jìn)入到success.jsp頁(yè)面。

登錄成功頁(yè)面

5、當(dāng)我們登錄成功之后,可以再次訪(fǎng)問(wèn)這個(gè)/success頁(yè)面以及應(yīng)用中其它的authc頁(yè)面,而不會(huì)重定向到login.jsp頁(yè)面

6、點(diǎn)擊退出登錄之后,我們?nèi)孕柙俅蔚卿洸拍茉L(fǎng)問(wèn)其它頁(yè)面。

總結(jié)

shiro可以做的不僅僅如圖演示,我已經(jīng)簡(jiǎn)化了很多這次操作,但仍然占據(jù)了很大篇幅。感興趣的朋友可以嘗試擴(kuò)充更多。如以下可以擴(kuò)充的地方

  • Realm從數(shù)據(jù)庫(kù)中獲取信息
  • Shiro角色授權(quán)
  • 額外的shiro配置等,緩存配置,多Realm配置
  • Shiro源碼分析
  • Shiro過(guò)濾器分析

本篇主要演示了如何在Spring或Spring MVC中集成Shiro,進(jìn)行請(qǐng)求攔截,認(rèn)證操作,最后對(duì)程序進(jìn)行了測(cè)試。

參考:
shiro入門(mén)教程
Idea配置Tomcat服務(wù)器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評(píng)論 6 342
  • 一、架構(gòu) 要學(xué)習(xí)如何使用Shiro必須先從它的架構(gòu)談起,作為一款安全框架Shiro的設(shè)計(jì)相當(dāng)精妙。Shiro的應(yīng)用...
    ITsupuerlady閱讀 3,617評(píng)論 4 32
  • 提前整理,只帶“必需”品,不裝“萬(wàn)一”品,時(shí)間久了你會(huì)發(fā)現(xiàn)一個(gè)精致且聰明的女人一個(gè)小包足以。 【0509今日話(huà)題】...
    撿到蜜罐的熊閱讀 283評(píng)論 0 0
  • 2017年7月25日 23:14:19 囚徒健身 完成舉腿2組 每次10個(gè)。 完成墻壁俯臥撐2組,每組20個(gè)。 額...
    嘻嘻君日記閱讀 478評(píng)論 0 0

友情鏈接更多精彩內(nèi)容