shiro学习(13):springMVC结合shiro完成认证

工具idea

先看看数据库

shiro_role_permission

数据

shiro_user

shiro_user_role

数据

在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>geyaoshiro</groupId><artifactId>geyaoshiro</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>geyaoshiro Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.11.RELEASE</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.2.3</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.8.RELEASE</version></dependency></dependencies><build><finalName>geyaoshiro</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

controller包

LoginController

package controller;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class LoginController {@RequestMapping("gologin.html")public String goLogin(){return "login";}@RequestMapping("login.html")public String login(String username, String password, HttpServletRequest request) {System.out.println("nihao");Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken(username, password);try {subject.login(token);return "redirect:index.html";}catch (AuthenticationException e){e.printStackTrace();request.setAttribute("error","用户名或者密码错误");return "login";}}
}

PageController

package controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class PageController {@RequestMapping("index")public String index(){return "Pages/index";}@RequestMapping("error.html")public String error(){return "Pages/error";}}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = error,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=error
log4j.logger.java.sql.Connection=error
log4j.logger.java.sql.Statement=error
log4j.logger.java.sql.PreparedStatement=error
log4j.logger.java.sql.ResultSet=error### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

shiro-web.ini

[users]
root = secret,admin
guest = guest,guest
test = 123456,guest,test[roles]
admin = *
guest=user:list
test=menu:list,menu:add

spring-config.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:classpath="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><bean id="iniRealm" class="org.apache.shiro.realm.text.IniRealm"><constructor-arg name="resourcePath" value="classpath:shiro-web.ini"/></bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="iniRealm"></property>
</bean><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager"></property><property name="loginUrl" value="/gologin.html"></property><property name="successUrl" value="/index.html"></property><property name="unauthorizedUrl" value="/error.html"></property><property name="filterChainDefinitions"><value>[urls]/login.html=anon/gologin.html=anon/index.html=authc/role.html=authc,roles[admin]/menu/**=authc,roles[admin],perms[menu:*]</value></property></bean>
</beans>

error.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 14:05To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
权限不足
</body>
</html>

index.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 13:53To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
首页
</body>
</html>

login.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 13:46To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<form action="/login.html" method="post">用户名:<input type="text" name="username"/><br/>密码:<input type="password" name="password"/><br/><input type="submit" value="登录"/>${error}
</form>
</body>
</html>

springMVC-servlet

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><mvc:annotation-driven /><!-- 定义要扫描 controller的包--><context:component-scan base-package="controller"></context:component-scan><!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --><!--指定视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 视图的路径 --><property name="prefix" value="/WEB-INF/"/><!-- 视图名称后缀  --><property name="suffix" value=".jsp"/></bean></beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-config.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet>
<servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.html</url-pattern>
</servlet-mapping><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>*.html</url-pattern></filter-mapping>
</web-app>

运行结果

回车

回车

登录错误

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/423562.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用小程序·云开发两天搭建mini论坛丨实战

笔者最近涉猎了小程序相关的知识&#xff0c;于是利用周末时间开发了一款类似于同事的小程序&#xff0c;深度体验了小程序云开发模式提供的云函数、数据库、存储三大能力。关于云开发&#xff0c;可参考文档&#xff1a;小程序云开发。 个人感觉云开发带来的最大好处是鉴权流程…

shiro学习(14):springMVC结合shiro完成认证

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3…

mysql聚合函数rollup和cube

转:https://blog.csdn.net/liuxiao723846/article/details/48970443 一、with rollup&#xff1a; with rollup 通常和group by 语句一起使用&#xff0c;是根据维度在分组的结果集中进行聚合操作。——对group by的分组进行汇总。 假设用户需要对N个纬度进行聚合查询操作&am…

Spring Boot----监控管理

用来监控spring 项目信息的 1、创建项目 1.1 启动项目 转载于:https://www.cnblogs.com/yanxiaoge/p/11400734.html

shiro学习(15):使用注解实现权限认证和后台管理

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3…

用小程序·云开发打造功能全面的博客小程序丨实战

用小程序云开发将博客小程序常用功能“一网打尽” 本文介绍mini博客小程序的详情页的功能按钮如何实现&#xff0c;具体包括评论、点赞、收藏和海报功能&#xff0c;这里记录下整个实现过程和实际编码中的一些坑。 评论、点赞、收藏功能 实现思路 实现文章的一些操作功能&#…

shiro学习(16):使用注解实现权限认证和后台管理二

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

Javascript玩转Prototype(一)——先谈C#原型模式

在《Javascript玩转继承&#xff08;二&#xff09;》中&#xff0c;我使用了原型继承法来实现Javascript的继承&#xff0c;那原型究竟奥秘何在。在这篇文章中&#xff0c;我就主要针对原型来展开讨论。 抛开Javascript&#xff0c;我们先来看我们熟悉的常规的面向对象语言。…

HDU 1176 免费馅饼 (动态规划、另类数塔)

免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 76293 Accepted Submission(s): 26722 Problem Description 都说天上不会掉馅饼&#xff0c;但有一天gameboy正走在回家的小径上&#xff0c;忽然天上掉…

shiro学习(17):easyui布局测试

工具sublime <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Document</title><link href"themes/black/easyui.css" rel"stylesheet" /><link href"themes…

正则表达式测试工具

这个工具最开始是年前写的&#xff0c;原文见如下地址&#xff1a;写了一个测试正则表达式的小工具 后来快过年的时候一直忙着给票贩子送钱去了&#xff0c;没有把它写完&#xff0c;今天抽空把一些细节的功能完成了一下&#xff0c;感兴趣的朋友可以下载试用&#xff1a;点击…

shiro学习(18):使用注解实现权限认证和后台管理三

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

shiro学习(19): 拦截器

1 拦截器介绍 Shiro使用了与Servlet一样的Filter接口进行扩展&#xff1b;所以如果对Filter不熟悉可以参考《Servlet3.1规范》http://www.iteye.com/blogs/subjects/Servlet-3-1了解Filter的工作原理。首先下图是Shiro拦截器的基础类图&#xff1a; 1、NameableFilter Name…

关于云开发新服务“实时数据推送”,你需要了解的全在这了!

“微信小程序工程师邓坤力带你了解如何利用千呼万唤始出来的云开发实时数据推送服务打造生动的小程序和小游戏&#xff01;” 在数据库在小程序云开发中的应用一文中&#xff0c;我们了解到实时数据推送作为云开发即将上线的一项新能力&#xff0c;主要指客户端使用官方SDK发起…

java实现二分查找-两种方式

转:https://blog.csdn.net/maoyuanming0806/article/details/78176957 二分查找是一种查询效率非常高的查找算法。又称折半查找。 起初在数据结构中学习递归时实现二分查找&#xff0c;实际上不用递归也可以实现&#xff0c;毕竟递归是需要开辟额外的空间的来辅助查询。本文就…

shiro学习(20): 自定义过滤规则

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

css 一些 常用布局

div骨架 Code<div id"header">ss</div> <div id"container"> <div id"content">ss</div> <div id"side">ss</div> </div> <div id"pagefooter"…

shiro学习(21):动态添加验证规则1

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

shiro学习(22):动态添加验证规则2

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

shiro学习(23):动态添加验证规则3

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…