1.下载spring security的最新版本,工程下载的是3.1
2. 新建工程,结构如下:
其中,涉及到的jar包可以在spring-security包中的例子中获取
3、配置spring-security.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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
<!--配置认证管理器,只有用户名为user,密码为user的用户,角色为ROLE_USER可访问指定的资源 -->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
4.配置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>springSecurity</display-name>
<!--******************************** -->
<!--*******log4j日志信息的配置****** -->
<!--******************************* -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond,可以不设置 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!--******************************** -->
<!--*******spring bean的配置******** -->
<!--******************************* -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--******************************** -->
<!--*******字符集 过滤器************ -->
<!--******************************* -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--******************************** -->
<!--*******session的配置************ -->
<!--******************************* -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- SpringSecurity必须的begin -->
<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>
<!-- SpringSecurity必须的end -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.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>
<h1>这里是首页,欢迎你!</h1>
<%
String[] str = session.getValueNames();
for(int i=0;i<str.length;i++){
out.println("key=="+str[i]);
out.println("value=="+session.getAttribute(str[i]));
}
%>
</body>
</html>
6部署应用,在首次浏览index.jsp时,由于没登录,spring security会自动生成登录页面,页面内容如下:
7输入用户名和密码,user,则进入首页
至此,简单的权限控制完成,在index页面中通过session可以看到存入session中的用户信息。
- 大小: 87.3 KB
- 大小: 27.1 KB
- 大小: 83.1 KB
分享到:
相关推荐
hello大家好,这里是X,今天这篇博文带来的是SpringBoot安全管理:SpringSecurity,讲到安全管理,不得不说几乎所有的大型项目开发必备之一,而且有了它,对项目的安全也起到了非常大的效果,可以说是项目搭建的必备...
NULL 博文链接:https://clongjava.iteye.com/blog/1453648
每篇文章的源代码示例均使用最新的 Spring 5.1.0 RELEASE、JDK 8 和 Maven 3.2+ 开发。 Spring MVC 入门/基础 - 在本文中,我们将学习如何使用 Spring MVC 5 +、JSP、Maven 构建工具和 Eclipse IDE 创建一个简单的 ...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...
WebService CXF学习——入门篇 1.CXF由来 2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF...
[全网最详细的一篇SpringCloud总结] () [feign] () [Spring Security 真正的前后分离实现] () [Spring Cloud Gateway] () [SpingCloud Gateway网关核心概念和原理] () [基于Redis实现Spring Cloud Gateway] () ...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...
│ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...
如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件机制的简单实现 我对J2EE和.NET的一点理解 难分难舍的DSO(一) InternalsVisibleToAttribute,友元程序集访问属性 Essential .NET 读书笔记 [第一部分] ...
│ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...
│ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...
│ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...
│ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini │ │ │ 考题_1.doc │ ...