java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downloads.html。不过我的项目里的jar包比较旧点了,是从以前项目抽取出来的,我的工程结构图如下:

如果是maven项目则可以使用下面的pom.xml:

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><!-- Log --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- Struts2 --><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.18</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.16.3</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.16.3</version></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId><version>3.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><version>3.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId><version>3.2.4.RELEASE</version></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.0.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.0.2</version></dependency><!-- Database --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.31</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies>

第一个实例:

第一个例子是最基本,最简单的,我第一次接触springsecurity时候觉得这个技术真惊艳,不过现在感觉也就那么回事了。

我首先编写的是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>SpringSecurityPrj</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param><filter><filter-name>springSecurityFilterChain</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>springSecurityFilterChain</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

接下来编写的是applicationContext-security.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"xmlns:beans="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.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.2.xsd"><!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 --><http auto-config="true"><intercept-url pattern="/**" access="ROLE_USER"/></http><!-- 认证管理器。用户名密码都集成在配置文件中 --> <authentication-manager><authentication-provider><user-service><user name="sharp" password="sharp" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager>
</beans:beans>

另外我新建了一个index.jsp文件,作用是登录成功后返回到index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录首页</title>
</head>
<body>
<span color="red">登录成功!</span>
</body>
</html>

当我们在浏览器地址栏里输入下面的url:

http://localhost:8080/SpringSecurityPrj/

我们就可以再浏览器里看到用户登录界面:

呵呵,内置的登录页面,挺好玩的。没有使用过springsecurity可能还没发现我在那里配置用户名和密码吧,看下面一段代码,这里就是用户名和密码:

<user name="sharp" password="sharp" authorities="ROLE_USER"/>

测试一:

我们录入用户名:admin;密码:admin,然后点击提交查询,最终页面如下:

登录失败了哦!

测试二:我们录入用户名:sharp;密码:sharp;如下图:

点击提交查询后,页面如下:

页面跳转到index.jsp页面,登录成功了。

哈哈,用这个做登录是不是很easy啊!

在第一个实例基础上我做了第二个实例。

第二个实例:

第一个例子里的登录页面是springsecurity的默认页面,这种死板的页面满足不了千变万化的用户需求,因此这个实例里我将自定义登录界面,这里我们还要加入几个jar包,最新的lib包下的目录如下所示:

新建一个login.jsp页面代码如下:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body onLoad="document.f.j_username.focus();">
<c:if test="${not empty param.login_error}"><font color="red">登录失败,请重试.<br/><br/>原因:<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/></font>
</c:if>
<form name="f" action="<c:url value='j_spring_security_check'/>" method="POST"><table><tr><td>用户名:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/></td></tr><tr><td>密     码:</td><td><input type='password' name='j_password'></td></tr><tr><td><input type="checkbox" name="_spring_security_remember_me"></td><td>两周内自动登录</td></tr><tr><td colspan='2' align="center"><input name="submit" type="submit">  <input name="reset" type="reset"></td></tr></table>
</form>
</body>
</html>

修改applicationContext-security.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"xmlns:beans="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.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.2.xsd"><!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 --><http auto-config="true"><intercept-url pattern="/login.jsp*"  access="IS_AUTHENTICATED_ANONYMOUSLY" /><intercept-url pattern="/**" access="ROLE_USER"/><form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> </http><!-- 认证管理器。用户名密码都集成在配置文件中 --> <authentication-manager><authentication-provider><user-service><user name="sharp" password="sharp" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager><!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: --> <beans:bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><beans:property name="basename"  value="classpath:org/springframework/security/messages_zh_CN"/>  </beans:bean>
</beans:beans>

我们在浏览器地址栏里输入下面的url,点击回车,界面如下:

测试一:

我们录入用户名:admin;密码:admin,然后点击提交查询,最终页面如下:

登录失败!

测试二:我们录入用户名:sharp;密码:sharp;如下图:

点击提交查询,结果如下:

第三个实例:

只要是接触过权限管理的程序员都知道,一般的权限管理都有角色的概念,但是传统的角色都是在数据库建模,然后用编程的方式来实现的。在springsecurity里面就有角色的概念,用起来也很方便,上面的例子里我们使用了一个角色ROLE_USER,现在我们添加一个角色ROLE_ADMIN,我们修改applicationContext-security.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"xmlns:beans="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.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.2.xsd"><!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 --><http auto-config="true"><intercept-url pattern="/login.jsp*"  access="IS_AUTHENTICATED_ANONYMOUSLY" /><!-- 增加 ROLE_ADMIN角色--><intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/><intercept-url pattern="/**" access="ROLE_USER"/><form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> </http><!-- 认证管理器。用户名密码都集成在配置文件中 --> <authentication-manager><authentication-provider><user-service><!-- 添加ROLE_ADMIN角色 --><user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/><user name="sharp" password="sharp" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager><!-- 指定中文资源 。默认命名空间是security,所以要加前缀beans: --> <beans:bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><beans:property name="basename"  value="classpath:org/springframework/security/messages_zh_CN"/>  </beans:bean>
</beans:beans>

另外我新建一个admin.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin 管理界面</title>
</head>
<body>
<p style="color:red">admin.jsp页面</p>
</body>
</html>

修改下index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录首页</title>
</head>
<body>
<span color="red">登录成功!</span>
<br/>
<a href="admin.jsp">admin.jsp</a>
</body>
</html>

测试一:

我们输入的用户名:sharp;密码:sharp,登录成功了,我们进入到了页面index.jsp:

点击admin.jsp链接,结果如下:

sharp用户没有ROLE_ADMIN角色的权限,所以sharp访问不了admin.jsp页面。

测试二:

我们输入的用户名:admin;密码:admin,登录成功了,我们进入到了页面index.jsp(如上图),

然后

点击admin.jsp链接,结果如下:

用户admin是可以访问admin.jsp页面。

好了,今天学习结束了!

总结下:今天都是具体操作,而且这些操作在网上多的不得了,不过我想学springsecurity都得从这一步开始,现在我对springsecurity理解还不深,整篇文章都是如何去编码,而没有一些知识的讲解,我会尽全力一步步深入,做软件最好还是知其所以然,明天看能不能研究原理,下一篇由springsecurity和数据库的结合开始。

转载于:https://www.cnblogs.com/longshiyVip/p/5055184.html

SpringSecurity的简单应用(一)相关推荐

  1. SpringSecurity的简单概述以及配置SpringSecurity的默认登录页面

    SpringSecurity的简单概述 是什么:SpringSecurity融合Spring技术栈,提供JavaEE应 用的整体安全解决方案:提供全面的安全服务 有什么用:可以进行身份验证,就是证明你 ...

  2. SpringSecurity的简单使用使用案列说明

    SpringSecurity Spring Security是 Spring提供的安全认证服务的框架. 使用Spring Security可以帮助我们来简化认证 和授权的过程.官网:https://s ...

  3. SpringSecurity的简单使用

    目录 概述 HelloWorld 依赖 写一个Controller 访问接口 用户认证 设置登录的用户名和密码 通过配置文件 通过配置类 自定义编写实现类 查询数据库的用户认证 自定义登录页面 用户授 ...

  4. SpringSecurity 简单使用

    SpringSecurity 简单使用 在 Web 开发中安全是不可忽视的问题(软件安全技术!),现在从 SpringSecurity 和 Shiro 两个框架来学习一下安全框架在 Web 应用中的使 ...

  5. Spring Security详细讲解(JWT+SpringSecurity登入案例)

    本篇博文目录: 一.SpringSecurity简介 1.SpringSecurity 2.SpringSecurity相关概念 二.认证和授权 1.认证 (1) 使用SpringSecurity进行 ...

  6. JAVA-WBE——spring security 3.2 认证-学习笔记2

    2019独角兽企业重金招聘Python工程师标准>>> spring-security.xml 简单实现用户登录 (不连接数据库) <?xml version="1. ...

  7. 【Java从0到架构师】项目实战 - 会话管理、EhCache、JWT、权限管理 Shiro、打包部署

    项目实战 - 权限管理 会话管理 客户端身份认证 - 基于 Cookie.Session 客户端身份验证 - 基于 token EhCache - 简单的缓存框架 JWT - 基于 JSON 的 to ...

  8. SpringBoot使用security实现OAuth2

    SpringBoot使用security实现OAuth2 OAuth2 OAuth是一个开放标准,允许用户授权地方应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方应用或者 ...

  9. SpringSecurity使用SpringBoot简单使用

    SpringSecurity使用SpringBoot简单使用(一) 先来回顾下最开始我们不用框架时是怎么做认证授权的, 1.不用框架认证流程 1.客户端发送请求,controller层将请求对象封装成 ...

最新文章

  1. 动态线条,随鼠标移动吸附效果
  2. 安卓抓包工具 linux,Android 下使用tcpdump网络抓包方法
  3. 基于DataTabel的增删改查
  4. 51nod 1847 奇怪的数学题(数论/min25筛/杜教筛/斯特林数)
  5. jieba分词提取小说人名
  6. 关于public class 类名{ public static void main(String[] args)}的一些说明
  7. Hackerlands:城市Hackerspaces的农村版本
  8. PHP 显示信息到控制台console
  9. Fastjson反序列化泛型类型时候的一个问题
  10. Ubuntu的常识使用了解2
  11. 解决ORA-27103:internal error错误一例
  12. Javascript 监控键盘输入事件
  13. 软件测试作业随笔之二:Homework 2
  14. 关于凸包算法和叉积的应用
  15. moodle安装图解
  16. 100行python代码实现细胞自动机(康威生命游戏)
  17. [unity3d插件]插件效果以及下载
  18. 计算机内存怎么与频率匹配,如何进行内存频率设置?内存频率设置方法
  19. optaplanner学习笔记(十)约束配置:动态调整约束权重
  20. js--动态生成表格

热门文章

  1. R语言如何画个性化词云图
  2. mysql 添加int列_在MySQL中现有的int列的值中添加字符?
  3. 惠普打印机怎么无线连接电脑_惠普打印机打印中突然停止怎么办?
  4. id ajax,AJAX_ajax.js里面有内容显示效果,根据ID,ajax.js里面有内容显示效果,根 - phpStudy...
  5. php url 删除参数错误,为什么在通过改型2按id删除数据时会出现IllegalArgumentException错误?以及如何修复它...
  6. 实现php实现价格的排序,PHP实现二维数组排序(按照数组中的某个字段)
  7. matlab pdist2函数官方详解
  8. python创建学生类和教师类_创建StudentServices类
  9. java继承和多态的实验报告_JAVA,继承和多态实验报告
  10. python中split的用法取第二个分片_python中的split()函数的用法