这里用的是SpringMVC-3.2.4和Shiro-1.2.2,演示样例代码例如以下
首先是web.xml
以下是用于显示Request method ‘GET‘ not supported的//WebRoot//WEB-INF//405.html
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>405.html</title>
-
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
-
</head>
-
<body>
-
<font color="blue">
-
Request method ‘GET‘ not supported
-
<br/><br/>
-
The specified HTTP method is not allowed for the requested resource.
-
</font>
-
</body>
-
</html>
以下是同意匿名用户訪问的//WebRoot//login.jsp
-
<%@ page language="java" pageEncoding="UTF-8"%>
-
-
<script type="text/javascript">
-
<!--
-
function reloadVerifyCode(){
-
document.getElementById(‘verifyCodeImage‘).setAttribute(‘src‘, ‘${pageContext.request.contextPath}/mydemo/getVerifyCodeImage‘);
-
}
-
//-->
-
</script>
-
-
<div style="color:red; font-size:22px;">${message_login}</div>
-
-
<form action="<%=request.getContextPath()%>/mydemo/login" method="POST">
-
姓名:<input type="text" name="username"/><br/>
-
密码:<input type="text" name="password"/><br/>
-
验证:<input type="text" name="verifyCode"/>
-
-
<img id="verifyCodeImage" onclick="reloadVerifyCode()" src="<%=request.getContextPath()%>/mydemo/getVerifyCodeImage"/><br/>
-
<input type="submit" value="确认"/>
-
</form>
以下是用户登录后显示的//WebRoot//main.jsp
-
<%@ page language="java" pageEncoding="UTF-8"%>
-
普通用户可訪问<a href="<%=request.getContextPath()%>/mydemo/getUserInfo" target="_blank">用户信息页面</a>
-
<br/>
-
<br/>
-
管理员可訪问<a href="<%=request.getContextPath()%>/admin/listUser.jsp" target="_blank">用户列表页面</a>
-
<br/>
-
<br/>
-
<a href="<%=request.getContextPath()%>/mydemo/logout" target="_blank">Logout</a>
以下是仅仅有管理员才同意訪问的//WebRoot//admin//listUser.jsp
-
<%@ page language="java" pageEncoding="UTF-8"%>
-
This is listUser.jsp
-
<br/>
-
<br/>
-
<a href="<%=request.getContextPath()%>/mydemo/logout" target="_blank">Logout</a>
以下是普通的登录用户所同意訪问的//WebRoot//user//info.jsp
-
<%@ page language="java" pageEncoding="UTF-8"%>
-
当前登录的用户为${currUser}
-
<br/>
-
<br/>
-
<a href="<%=request.getContextPath()%>/mydemo/logout" target="_blank">Logout</a>
以下是//src//log4j.properties
-
-
log4j.rootLogger=DEBUG,CONSOLE
-
-
log4j.logger.java.sql=DEBUG
-
log4j.logger.org.apache.shiro=DEBUG
-
log4j.logger.org.apache.commons=DEBUG
-
log4j.logger.org.springframework=DEBUG
-
-
-
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
-
log4j.appender.CONSOLE.Threshold=DEBUG
-
log4j.appender.CONSOLE.Target=System.out
-
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
-
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}.%M]%m%n
以下是//src//applicationContext.xml
-
<?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-3.2.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
-
-
-
<context:component-scan base-package="com.jadyer"/>
-
-
-
<mvc:annotation-driven/>
-
-
-
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/"/>
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
-
-
<mvc:view-controller path="/" view-name="forward:/login.jsp"/>
-
-
-
-
-
<mvc:resources mapping="/js/**" location="/resources/js/"/>
-
<mvc:resources mapping="/css/**" location="/resources/css/"/>
-
<mvc:resources mapping="/WEB-INF/**" location="/WEB-INF/"/>
-
-
-
-
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
-
<property name="exceptionMappings">
-
<props>
-
-
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">WEB-INF/error_fileupload</prop>
-
-
<prop key="java.lang.Throwable">WEB-INF/500</prop>
-
</props>
-
</property>
-
</bean>
-
-
-
<bean id="myRealm" class="com.jadyer.realm.MyRealm"/>
-
-
-
-
-
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
-
<property name="realm" ref="myRealm"/>
-
</bean>
-
-
-
-
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
-
-
<property name="securityManager" ref="securityManager"/>
-
-
<property name="loginUrl" value="/"/>
-
-
-
-
-
<property name="unauthorizedUrl" value="/"/>
-
-
-
-
-
-
<property name="filterChainDefinitions">
-
<value>
-
/mydemo/login=anon
-
/mydemo/getVerifyCodeImage=anon
-
/main**=authc
-
/user/info**=authc
-
/admin/listUser**=authc,perms[admin:manage]
-
</value>
-
</property>
-
</bean>
-
-
-
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
-
-
-
-
-
-
<!--
-
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
-
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
-
<property name="securityManager" ref="securityManager"/>
-
</bean>
-
-->
-
</beans>
以下是自己定义的Realm类----MyRealm.java
以下是处理用户登录的LoginController.java
-
package com.jadyer.controller;
-
-
import java.awt.Color;
-
import java.awt.image.BufferedImage;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.commons.lang3.StringUtils;
-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
-
import org.apache.commons.lang3.builder.ToStringStyle;
-
import org.apache.shiro.SecurityUtils;
-
import org.apache.shiro.authc.AuthenticationException;
-
import org.apache.shiro.authc.ExcessiveAttemptsException;
-
import org.apache.shiro.authc.IncorrectCredentialsException;
-
import org.apache.shiro.authc.LockedAccountException;
-
import org.apache.shiro.authc.UnknownAccountException;
-
import org.apache.shiro.authc.UsernamePasswordToken;
-
import org.apache.shiro.subject.Subject;
-
import org.apache.shiro.web.util.WebUtils;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
import org.springframework.web.servlet.view.InternalResourceViewResolver;
-
-
import com.jadyer.util.VerifyCodeUtil;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@Controller
-
@RequestMapping("mydemo")
-
public class LoginController {
-
-
-
-
@RequestMapping("/getVerifyCodeImage")
-
public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
-
-
response.setHeader("Pragma", "no-cache");
-
response.setHeader("Cache-Control", "no-cache");
-
response.setDateHeader("Expires", 0);
-
String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);
-
-
request.getSession().setAttribute("verifyCode", verifyCode);
-
System.out.println("本次生成的验证码为[" + verifyCode + "],已存放到HttpSession中");
-
-
response.setContentType("image/jpeg");
-
BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null);
-
-
ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());
-
}
-
-
-
-
-
-
@RequestMapping(value="/login", method=RequestMethod.POST)
-
public String login(HttpServletRequest request){
-
String resultPageURL = InternalResourceViewResolver.FORWARD_URL_PREFIX + "/";
-
String username = request.getParameter("username");
-
String password = request.getParameter("password");
-
-
String verifyCode = (String)request.getSession().getAttribute("verifyCode");
-
-
String submitCode = WebUtils.getCleanParam(request, "verifyCode");
-
System.out.println("用户[" + username + "]登录时输入的验证码为[" + submitCode + "],HttpSession中的验证码为[" + verifyCode + "]");
-
if (StringUtils.isEmpty(submitCode) || !StringUtils.equals(verifyCode, submitCode.toLowerCase())){
-
request.setAttribute("message_login", "验证码不对");
-
return resultPageURL;
-
}
-
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
-
token.setRememberMe(true);
-
System.out.println("为了验证登录用户而封装的token为" + ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE));
-
-
Subject currentUser = SecurityUtils.getSubject();
-
try {
-
-
-
-
System.out.println("对用户[" + username + "]进行登录验证..验证開始");
-
currentUser.login(token);
-
System.out.println("对用户[" + username + "]进行登录验证..验证通过");
-
resultPageURL = "main";
-
}catch(UnknownAccountException uae){
-
System.out.println("对用户[" + username + "]进行登录验证..验证未通过,未知账户");
-
request.setAttribute("message_login", "未知账户");
-
}catch(IncorrectCredentialsException ice){
-
System.out.println("对用户[" + username + "]进行登录验证..验证未通过,错误的凭证");
-
request.setAttribute("message_login", "password不对");
-
}catch(LockedAccountException lae){
-
System.out.println("对用户[" + username + "]进行登录验证..验证未通过,账户已锁定");
-
request.setAttribute("message_login", "账户已锁定");
-
}catch(ExcessiveAttemptsException eae){
-
System.out.println("对用户[" + username + "]进行登录验证..验证未通过,错误次数过多");
-
request.setAttribute("message_login", "username或password错误次数过多");
-
}catch(AuthenticationException ae){
-
-
System.out.println("对用户[" + username + "]进行登录验证..验证未通过,堆栈轨迹例如以下");
-
ae.printStackTrace();
-
request.setAttribute("message_login", "username或password不对");
-
}
-
-
if(currentUser.isAuthenticated()){
-
System.out.println("用户[" + username + "]登录认证通过(这里能够进行一些认证通过后的一些系统參数初始化操作)");
-
}else{
-
token.clear();
-
}
-
return resultPageURL;
-
}
-
-
-
-
-
-
@RequestMapping("/logout")
-
public String logout(HttpServletRequest request){
-
SecurityUtils.getSubject().logout();
-
return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/";
-
}
-
}
以下是处理普通用户訪问的UserController.java
-
package com.jadyer.controller;
-
-
import javax.servlet.http.HttpServletRequest;
-
-
import org.springframework.stereotype.Controller;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
-
@Controller
-
@RequestMapping("mydemo")
-
public class UserController {
-
@RequestMapping(value="/getUserInfo")
-
public String getUserInfo(HttpServletRequest request){
-
String currentUser = (String)request.getSession().getAttribute("currentUser");
-
System.out.println("当前登录的用户为[" + currentUser + "]");
-
request.setAttribute("currUser", currentUser);
-
return "/user/info";
-
}
-
}
最后是用于生成登录验证码的VerifyCodeUtil.java
-
package com.jadyer.util;
-
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.Graphics;
-
import java.awt.image.BufferedImage;
-
import java.util.Random;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public class VerifyCodeUtil {
-
-
-
-
public static final int TYPE_NUM_ONLY = 0;
-
-
-
-
-
public static final int TYPE_LETTER_ONLY = 1;
-
-
-
-
-
public static final int TYPE_ALL_MIXED = 2;
-
-
-
-
-
public static final int TYPE_NUM_UPPER = 3;
-
-
-
-
-
public static final int TYPE_NUM_LOWER = 4;
-
-
-
-
-
public static final int TYPE_UPPER_ONLY = 5;
-
-
-
-
-
public static final int TYPE_LOWER_ONLY = 6;
-
-
private VerifyCodeUtil(){}
-
-
-
-
-
private static Color generateRandomColor() {
-
Random random = new Random();
-
return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static BufferedImage generateImageCode(int type, int length, String excludeString, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){
-
String textCode = generateTextCode(type, length, excludeString);
-
return generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor);
-
}
-
-
-
-
-
-
-
-
-
-
public static String generateTextCode(int type, int length, String excludeString){
-
if(length <= 0){
-
return "";
-
}
-
StringBuffer verifyCode = new StringBuffer();
-
int i = 0;
-
Random random = new Random();
-
switch(type){
-
case TYPE_NUM_ONLY:
-
while(i < length){
-
int t = random.nextInt(10);
-
-
if(null==excludeString || excludeString.indexOf(t+"")<0) {
-
verifyCode.append(t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_LETTER_ONLY:
-
while(i < length){
-
int t = random.nextInt(123);
-
if((t>=97 || (t>=65&&t<=90)) && (null==excludeString||excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_ALL_MIXED:
-
while(i < length){
-
int t = random.nextInt(123);
-
if((t>=97 || (t>=65&&t<=90) || (t>=48&&t<=57)) && (null==excludeString||excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_NUM_UPPER:
-
while(i < length){
-
int t = random.nextInt(91);
-
if((t>=65 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_NUM_LOWER:
-
while(i < length){
-
int t = random.nextInt(123);
-
if((t>=97 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_UPPER_ONLY:
-
while(i < length){
-
int t = random.nextInt(91);
-
if((t >= 65) && (null==excludeString||excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
case TYPE_LOWER_ONLY:
-
while(i < length){
-
int t = random.nextInt(123);
-
if((t>=97) && (null==excludeString||excludeString.indexOf((char)t)<0)){
-
verifyCode.append((char)t);
-
i++;
-
}
-
}
-
break;
-
}
-
return verifyCode.toString();
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){
-
-
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-
-
Graphics graphics = bufferedImage.getGraphics();
-
-
graphics.setColor(null==backColor ? generateRandomColor() : backColor);
-
graphics.fillRect(0, 0, width, height);
-
-
Random random = new Random();
-
if(interLine > 0){
-
int x = 0, y = 0, x1 = width, y1 = 0;
-
for(int i=0; i<interLine; i++){
-
graphics.setColor(null==lineColor ? generateRandomColor() : lineColor);
-
y = random.nextInt(height);
-
y1 = random.nextInt(height);
-
graphics.drawLine(x, y, x1, y1);
-
}
-
}
-
-
int fsize = (int)(height * 0.8);
-
int fx = height - fsize;
-
int fy = fsize;
-
-
graphics.setFont(new Font("Default", Font.PLAIN, fsize));
-
-
for(int i=0; i<textCode.length(); i++){
-
fy = randomLocation ? (int)((Math.random()*0.3+0.6)*height) : fy;
-
graphics.setColor(null==foreColor ?
generateRandomColor() : foreColor);
-
-
graphics.drawString(textCode.charAt(i)+"", fx, fy);
-
fx += fsize * 0.9;
-
}
-
graphics.dispose();
-
return bufferedImage;
-
}
-
}