搭建springmvc简单步骤如:http://www.cnblogs.com/grasp/p/9045242.html,这点就不在描述了。
新建和设置完工程的目录后,结构如下:
pom.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xuan</groupId> <artifactId>springMVCshiro</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- main version setting --> <junit.version>4.12</junit.version> <spring.version>4.3.7.RELEASE</spring.version> <shiro.version>1.4.0</shiro.version> <!-- tools version setting --> <javax.servlet-api.version>4.0.0-b07</javax.servlet-api.version> <javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version> <jstl.version>1.2</jstl.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 1.SPRING相关依赖 begin√ --> <!-- ①.spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <!--包含Spring框架基本的核心工具类 --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <!--springIoC(依赖注入)的基础实现 --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <!--spring 提供在基础 IoC 功能上的扩展服务 --> <version>${spring.version}</version> </dependency> <!-- ②.spring 持久层依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <!--封装了spring对于事物的控制 --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <!--包含对Spring对JDBC数据访问进行封装的所有类 --> <version>${spring.version}</version> </dependency> <!-- ③.spring web相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web </artifactId> <!--包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入 WebApplicationContext 特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类 --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <!--Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架 --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <!--提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中 --> <version>${spring.version}</version> </dependency> <!-- ④.spring test依赖√ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <!--与Junit单元测试、集成测试 --> <version>${spring.version}</version> </dependency> <!-- ⑤.spring 其它依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <!--spring面向切面编程,提供AOP(面向切面编程) --> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <!-- SPRING相关依赖 end --> <!-- 2.SERVLET WEB begin√ --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId><!--j2ee web spec --> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${javax.servlet.jsp-api.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <!--JSTL标签类 --> <version>${jstl.version}</version> </dependency> <!-- SERVLET WEB end --> <!-- 3.安全框架SECURITY:shiro begin --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>${shiro.version}</version> </dependency> <!-- 安全框架SECURITY:shiro end --> </dependencies> <build> <finalName>springMVCshiro</finalName> </build> </project>
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>springMVCshiro</display-name> <servlet> <servlet-name>springMVCshiro</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc/spring-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVCshiro</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc/spring-context.xml,classpath:spring/spring-shiro.xml</param-value> </context-param> <!-- shiro 过滤器 start --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <!-- 设置true由servlet容器控制filter的生命周期 --> <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> <!-- shiro 过滤器 end --> </web-app>
spring-context.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <description>spring Configuration</description> <!-- 1.开启spring mvc注解模式 --> <mvc:annotation-driven enable-matrix-variables="true"/> <!-- 2.静态资源配置 (1)加入对静态资源的处理 js,jpg (2)允许使用"/"做整体映射 --> <mvc:default-servlet-handler/> <!-- 3.视图名称解析器:配置ViewResolver,定义跳转的文件的前后缀 。 可以用多个ViewResolver。 使用order属性排序。 InternalResourceViewResolver放在最后。 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 4.扫描web相关的Bean 使用Annotation自动注册Bean,只扫描@Controller --> <context:component-scan base-package="com.xuan.springmvcshiro.controller"/> <!-- 打开aop使用aop进行权限验证,shiro注解权限,角色等也需要开启 --> <aop:aspectj-autoproxy/> </beans>
spring-shiro.xml的内容:
<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-3.1.xsd"> <!-- Realm实现 --> <bean id="userRealm" class="com.xuan.springmvcshiro.filter.CustomRealm"> <!--<property name="credentialsMatcher" ref="credentialsMatcher"/>--> </bean> <!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="userRealm"/> </bean> <!-- Shiro的Web过滤器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="login"/> <!--<property name="successUrl" value="index"/>--> <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面--> <!--<property name="unauthorizedUrl" value="/403"/>--> <property name="filterChainDefinitions"> <value> /login = anon /loginUser = anon /adminRoles = roles["admin"] /** = authc </value> </property> </bean> <!--Shiro生命周期处理器--> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!--启用shiro注解--> <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>
MainController.java:
package com.xuan.springmvcshiro.controller; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresRoles; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class MainController { @RequestMapping(value = {"/", "/index"}) public ModelAndView Index(ModelMap model) { ModelAndView mav = new ModelAndView("index"); mav.addObject("message", "Hello World!"); return mav; } @RequestMapping("login") public String Login(ModelMap model) { return "login"; } @RequestMapping("adminRoles") public ModelAndView AdminRoles(ModelMap model) { ModelAndView mav = new ModelAndView("adminRoles"); mav.addObject("message", "adminRoles OK!"); return mav; } @RequestMapping(value = "loginUser", method = RequestMethod.POST) public String LoginUser(String userName, String passwd, Model model) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(userName, passwd); try { subject.login(token); return "redirect:/index"; } catch (UnknownAccountException e) { e.printStackTrace(); model.addAttribute("message", "用户名错误!"); return "login"; } catch (IncorrectCredentialsException e) { e.printStackTrace(); model.addAttribute("message", "密码错误"); return "login"; } } @RequiresPermissions("user:query") @RequestMapping(value = {"/testPermissions"}) public ModelAndView TestPermissions(ModelMap model) { ModelAndView mav = new ModelAndView("testShiro"); mav.addObject("message", "TestPermissions OK!"); return mav; } @RequiresRoles("admin") @RequestMapping(value = {"/testRoles"}) public ModelAndView TestRoles(ModelMap model) { ModelAndView mav = new ModelAndView("testShiro"); mav.addObject("message", "TestRoles OK!"); return mav; } }
CustomRealm.java的内容:
package com.xuan.springmvcshiro.filter; 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 java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CustomRealm extends AuthorizingRealm { /** * 用户和密码记录 */ static public Map<String, String> userList = new HashMap<String, String>(); static { userList.put("admin", "123456"); userList.put("test", "123456"); } /** * 授权 * * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String userName = (String) principalCollection.getPrimaryPrincipal(); List<String> permissionList = new ArrayList<String>(); permissionList.add("user:add"); permissionList.add("user:delete"); if (userName.equals("admin")) { permissionList.add("user:query"); } SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addStringPermissions(permissionList); if (userName.equals("admin")) { info.addRole("admin"); } return info; } /** * 认证 * * @param authenticationToken * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String userName = (String) authenticationToken.getPrincipal(); if ("".equals(userName)) { return null; } if (!userList.containsKey(userName)) { return null; } String passWord = userList.get(userName); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName, passWord, this.getName()); return info; } }
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <html> <body> <h2>${message}</h2> <form method="post" action="testPermissions"><input type="submit" value="测试Permissions"></form> <form method="post" action="testRoles"><input type="submit" value="测试注解配置Roles"></form> <form method="post" action="adminRoles"><input type="submit" value="测试XML配置Roles"></form> </body> </html>
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body> <form id="form1" name="form1" method="post" action="loginUser"> <p align="center">用户登录</p> <table width="296" border="1" align="center"> <tr> <td width="98" height="34">用户名:</td> <td width="182"><label><input name="userName" type="text" id="userName"/></label></td> </tr> <tr> <td height="36">密码:</td> <td><label> <input name="passwd" type="password" id="passwd"/></label></td> </tr> <tr> <td height="35" colspan="2"><label> <input type="submit" name="Submit" value="提交"/> </label> <label> <input type="reset" name="Submit2" value="重置"/> </label>${message}</td> </tr> </table> </form> </body> </html>
testShiro.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${message} </body> </html>
adminRoles.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${message} </body> </html>
编译调试运行结果:
输入错误的用户名:
输入正确的用户名和密码(admin)结果:
测试权限和角色正常:
清除本地缓存后用test登陆,用得chrome浏览器:
可以登陆index.jsp
测试权限,抛出权限异常:
测试角色权限:
测试XML角色配置:
onAccessDenied