【Spring Security】之一:Eclipse搭建Spring Security项目
来源:互联网 发布:oracle数据库面试题 编辑:程序博客网 时间:2024/06/06 11:48
写在前面的话:本人小白,仅供参考。
一、原材料
1、Eclipse软件,直接从官网下载;
2、SpringFramework的全套jar包:spring-framework-4.1.7.RELEASE-dist.zip;
3、Spring Security的全套jar包:spring-security-4.0.1.RELEASE-dist.zip;
4、SpringFramework依赖的jar包:commons-logging-4.0.6.jar,log4j-1.2.16.jar;
二、工序
1、Eclipse建立Dynamic Web类型(动态网页)的工程,注意自动生成web.xml文件(当然创建工程的时候不生成也可以后续手动添加)。
工程目录如下:
将(一)中2、3、4项包含的jar包全部拷贝到WebContent/WEB-INF/lib下;
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
<span></span>xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
<span></span>id="WebApp_ID" version="3.1"> <display-name>security</display-name> <!-- - Location of the XML file that defines the root application context - Applied by ContextLoaderListener. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml /WEB-INF/config-security.xml </param-value> </context-param> <!-- 配置Security --> <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> <!-- 配置Spring的Web应用 --> <!-- - Loads the root application context of this web app at startup. - The application context is then available via - WebApplicationContextUtils.getWebApplicationContext(servletContext). --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>3、配置config-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-4.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <http><span></span><intercept-url pattern="/**" access="hasRole('USER')" /><span></span><form-login /><span></span><logout /> </http> <!-- koala: koala emu:emu wombat:wombat --> <authentication-manager> <authentication-provider> <password-encoder hash="md5"/> <user-service> <user name="koala" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" /> <user name="emu" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" /> <user name="wombat" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>4、配置applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans>5、编辑index.xml
<!doctype html><html><head> <title>Spring Security</title></head><body> <h1>Hello Sprig Security!</h1> <hr></body></html>
三、验收
至此,我们并没有写任何的java代码,只是配置了Spring Security到我们的web工程中来并配置了简单的登录验证。我们可以看到如下效果:
图1 在tomcat服务器运行index.xml文件 图2 用户名/密码不匹配时
图2 用户名/密码匹配时
0 0
- 【Spring Security】之一:Eclipse搭建Spring Security项目
- Spring security实战(1)-----项目搭建
- Spring Security 项目
- Spring MVC 项目搭建 -3- 快速 添加 spring security
- Spring security搭建与使用
- Spring Security
- Spring Security
- spring security
- spring security
- spring security
- spring security
- spring security
- spring security
- spring security
- spring security
- spring security
- Spring Security
- spring security
- 拓扑排序简单题
- Coins(多重背包+二进制优化)
- bzoj-3143 游走
- 九度oj 1445
- 黑马程序员——自学java基础第一天
- 【Spring Security】之一:Eclipse搭建Spring Security项目
- AndroidDevTools
- hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图
- 栈的压入、弹出序列
- 随手记
- Linxu命令与文件的搜索 - which, whereis, locate, find
- 进入最后的沟通阶段, 更需要一如既往地保持冷静的心态和真诚的态度, 继续负责地贡献自己的力量。
- Git本地基础操作
- Java之反射机制【入门版(4),实例解析】