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简单项目目录结 ...
随机推荐
- Python之路PythonThread,第三篇,进程3
python3 进程3 管道 在内存中开辟一个管道空间,对多个进程可见. 在通信形式上形成一种约束: linux 文件类型 b c d - ...
- Java中double转BigDecimal的注意事项
先上结论:不要直接用double变量作为构造BigDecimal的参数! 线上有这么一段Java代码逻辑: 1,接口传来一个JSON串,里面有个数字:57.3. 2,解析JSON并把这个数字保存在一个 ...
- java-接口的成员特点
1.成员变量: - 只能是常量,并且是静态的.公共的. - 默认修饰符:public static final - 建议:自己手动给出. 2.构造方法:接口没有构造方法. 3.成员方法: - 只能是抽 ...
- this语句的知识点第五点
对不起大家久等了 最后一点 第五点 给元素中的某一个事件绑定方法,当事件触发时,执行绑定的方法,方法中的this指向当前元素. funciton fn(){ console.log(this) } d ...
- (8)视图层参数request详解
PS:浏览器页面请求的都是get请求 PS:post请求是通过form表单,阿贾克斯发 request里面的常用方法 def index(request): print(request.META) # ...
- 《DSP using MATLAB》Problem 5.18
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% O ...
- LeetCode - Most Frequent Subtree Sum
Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...
- 【JVM】参数配置
[一]JVM参数配置释意 编号 配置项 例子 含义 备注 1 -Xmx -Xmx20m java应用最大可用内存为20M 整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小.持久代一般固 ...
- day 55 jQuery 之事件 绑定等
属性选择器: [attribute] [attribute=value]// 属性等于 列入 $("input[value='male']").prop('chekced') 注意 ...
- 递归和非递归分别实现求n的阶乘
思路:举例求6的阶乘,6*5*4*3*2*1.可以将5开始看成另一个整型变量n,用一个循环每次将n的值减少1,.而递归也是如此,每次调用函数的时候将变量减一就可以. 方法一:非递归 //非递归: #i ...