Apache Shiro簡單示例

        一、新建一個Maven的war工程,並在pom.xml中增長以下依賴。html

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>4.0.9.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-spring</artifactId>
 <version>1.2.3</version>
</dependency>
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
</dependency>
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-quartz</artifactId>
 <version>1.2.3</version>
</dependency>
<dependency>
 <groupId>commons-collections</groupId>
 <artifactId>commons-collections</artifactId>
 <version>3.2.1</version>
</dependency>
<dependency>
 <groupId>javax.servlet.jsp</groupId>
 <artifactId>jsp-api</artifactId>
 <version>2.2</version>
</dependency>

         二、在web.xml中增長以下內容。java

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring-core.xml</param-value>
</context-param>
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
 <filter-name>shiroFilter</filter-name>
 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 <async-supported>true</async-supported>
 <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>

        三、新增兩個頁面,分別爲login.jsp和index.jsp,主要用於展現登陸界面和主界面。web

        login.jspspring

<%@page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>shiro-spring登陸頁面</title>
<style>.error{color:red;}</style>
</head>
<body>
 <div class="error">${error}</div>
 <form action="" method="post">
  用戶名:<input name="username" type="text"> 密碼:<input
   name="password" type="password">
  <button type="submit">登陸</button>
 </form>
</body>
</html>

        index.jspapache

<%@page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<html>
<head>
<title>shiro-spring歡迎頁面</title>
</head>
<body>
 <shiro:guest>
  歡迎遊客訪問,<a href="${pageContext.request.contextPath}/login.jsp">點擊登陸</a><br/>
 </shiro:guest>
 <shiro:user>
  歡迎[<shiro:principal/>]登陸,<a href="${pageContext.request.contextPath}/logout">點擊退出</a><br/>
 </shiro:user>
</body>
</html>

        四、新增一個UserRealm類,用於實現用戶登陸操做的相關校驗。這裏爲了簡化,寫死了用戶名、密碼和鹽值。用戶名:admin,密碼:123456api

package org.cloud.shiro.realm;
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.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 {
  String username=(String) token.getPrincipal();
  SimpleAuthenticationInfo authenticationInfo=new SimpleAuthenticationInfo(username,"123456",ByteSource.Util.bytes(username+"8d78869f470951332959580424d4bf4f"),getName());
  return authenticationInfo;
 }
}

        五、新增spring-core.xml文件,主要用於shiro的配置。注意其中須要配置上面所編寫的UserRealm類。安全

<?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-4.0.xsd">
 <!-- Realm實現 -->
 <bean id="userRealm" class="org.cloud.shiro.realm.UserRealm" />
 <!-- 會話ID生成器 -->
 <bean id="sessionIdGenerator"
  class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator" />
 <!-- 會話Cookie模板 -->
 <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
  <constructor-arg value="sid" />
  <property name="httpOnly" value="true" />
  <property name="maxAge" value="180000" />
 </bean>
 <!-- 會話DAO -->
 <bean id="sessionDAO"
  class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
  <property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
  <property name="sessionIdGenerator" ref="sessionIdGenerator" />
 </bean>
 <!-- 會話驗證調度器 -->
 <bean id="sessionValidationScheduler"
  class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
  <property name="sessionValidationInterval" value="1800000" />
  <property name="sessionManager" ref="sessionManager" />
 </bean>
 <!-- 會話管理器 -->
 <bean id="sessionManager"
  class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  <property name="globalSessionTimeout" value="1800000" />
  <property name="deleteInvalidSessions" value="true" />
  <property name="sessionValidationSchedulerEnabled" value="true" />
  <property name="sessionValidationScheduler" ref="sessionValidationScheduler" />
  <property name="sessionDAO" ref="sessionDAO" />
  <property name="sessionIdCookieEnabled" value="true" />
  <property name="sessionIdCookie" ref="sessionIdCookie" />
 </bean>
 <!-- 安全管理器 -->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  <property name="realm" ref="userRealm" />
  <property name="sessionManager" ref="sessionManager" />
 </bean>

 <bean id="formAuthenticationFilter"
  class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
  <property name="usernameParam" value="username" />
  <property name="passwordParam" value="password" />
 </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="/index.jsp" />
  <property name="filters">
   <map>
    <entry key="authc" value-ref="formAuthenticationFilter"></entry>
   </map>
  </property>
  <property name="filterChainDefinitions">
   <value>
    /login.jsp = authc
    /logout = logout
    /** = user
   </value>
  </property>
 </bean>
 <!-- Shiro生命週期處理器 -->
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

        六、啓動項目後,訪問login.jsp後,會顯示登陸界面,輸入用戶名和密碼後,能夠進入到index.jsp頁面。固然點擊退出按鈕後,返回至登陸頁面中。若是未登陸系統,在地址欄中直接訪問index.jsp是會被返回至登陸頁面中的。session

        到此,一個簡單的shiro示例就搭建完成,你們能夠根據項目的實際須要擴展內容。本文主要簡單列舉了一下shiro的示例,主要爲shiro的首次學習者作一個開場示例,若要深刻掌握shiro,還需本身多學習,多實踐。app

相關文章
相關標籤/搜索