簡(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 。


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ù)。

增加必要的文件
在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)如圖

其它的頁(yè)面,可以自己隨便寫(xiě)。
測(cè)試
1、程序啟動(dòng)時(shí),直接進(jìn)入登錄頁(yè)面

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

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

4、登錄成功,登錄成功的時(shí)候,進(jìn)入到success.jsp頁(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è)試。