IntelliJ IDEA maven springmvc+shiro简单项目
搭建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
IntelliJ IDEA maven springmvc+shiro简单项目的更多相关文章
- Maven+SpringMVC+Dubbo 简单的入门demo配置
转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...
- 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置
之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...
- IntelliJ IDEA + Maven创建Java Web项目
1. Maven简介 相对于传统的项目,Maven 下管理和构建的项目真的非常好用和简单,所以这里也强调下,尽量使用此类工具进行项目构建, 它可以管理项目的整个生命周期. 可以通过其命令做所有相关的工 ...
- 17. IntelliJ IDEA + Maven创建Java Web项目
转自:https://www.cnblogs.com/Terry-Wu/p/8006475.html 1. Maven简介 相对于传统的项目,Maven 下管理和构建的项目真的非常好用和简单,所以这里 ...
- intellij idea maven springmvc 环境搭建
1. 新建maven 工程 intellij idea 默认已经集成了maven, 直接点击下一步 2. 配置文件修改 pom.xml 文件 <?xml version="1. ...
- Intellij IDEA+Maven+SpringMVC+HiBernate
转载请注明出处:Gaussic(一个致力于AI研究却不得不兼顾项目的研究生). 访问GitHub下载最新源码:https://github.com/gaussic/SpringMVCDemo
- eclipse建立springMVC 简单项目
http://jinnianshilongnian.iteye.com/blog/1594806 如何通过eclipse建立springMVC的简单项目,现在简单介绍一下. 工具/原料 eclip ...
- Intellij IDEA +MAVEN+Jetty实现SpringMVC简单查询功能
利用 Intellij IDEA +MAVEN+Jetty实现SpringMVC读取数据库数据并显示在页面上的简单功能 1 新建maven项目,配置pom.xml <project xmlns= ...
- IntelliJ IDEA 创建 Maven简单项目
创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...
随机推荐
- Android SO动态调试之IDA
1.上传并启动android_server(IDA的dbgsrv目录) 2.设置端口转发:adb forward tcp:23946 tcp:23946 3.调试模式启动应用:adb shell am ...
- Python之路,第三篇:Python入门与基础3
1, 布尔运算符 运算符; not and or not 运算符: 作用:逻辑取反 语法: not 表达式 例: not True # False not Fa ...
- NCPC 2016:简单题解
A .Artwork pro:给定N*M的白色格子,然后Q次黑棒,输出每次加黑棒后白色连通块的数量.(N,M<1e3, Q<1e4) sol:倒着离线做,并查集即可. (在线做法:http ...
- C语言--第一周作业评分和总结(5班)
作业链接:https://egdu.cnblogs.com/campus/hljkj/CS2017-5/homework/963 一.评分要求 * 要求1 (5分):博客中给出安装软件的截图(得分点1 ...
- 无用之flask
Oldboy s4 Flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收 ...
- import sys
目录 sys模块的常见函数列表 1.sys.argv 2.sys.platform 3.sys.path 4.sys.exit(n) sys模块提供了一系列有关Python运行环境的变量和函数. 回到 ...
- hdu3338 Kakuro Extension 最大流
If you solved problem like this, forget it.Because you need to use a completely different algorithm ...
- Python 3.5 in win10 pip install Orange3
http://www.lfd.uci.edu/%7Egohlke/pythonlibs/ 下载Orange3 以及 依赖包 注意网页上标出的Orange 的依赖,以及 https://github.c ...
- Extend a root LVM partition online
There is also a quick remedy for the emergency situation when your root partition runs out of disk s ...
- JS从数组中随机取出几个数组元素的方法
原文链接:http://caibaojian.com/js-get-random-elements-from-array.html js如何从一个数组中随机取出一个元素或者几个元素. 假如数组为· v ...