创建一个maven web项目,pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hisky</groupId> <artifactId>security1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>security1 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.1.4.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>security1</finalName> </build> </project>
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" version="2.5"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
applicationContext.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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <http pattern="/login.jsp" security="none" /> <http access-denied-page="/denied.jsp"> <form-login login-page="/login.jsp" default-target-url="/index.jsp" /> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" /><!--访问admin.js必须有admin权限--> <intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN" /><!--访问其他页面,必须有user或admin权限--> <session-management> <concurrency-control max-sessions="1" error-if-maximum-exceeded="false" /> </session-management> </http> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="admin" authorities="ROLE_ADMIN" /> <user name="test" password="test" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
login.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录</title> </head> <body> <form action="j_spring_security_check" method="POST"> <table> <tr> <td>用户:</td> <td><input type='text' name='j_username'></td> </tr> <tr> <td>密码:</td> <td><input type='password' name='j_password'></td> </tr> <tr> <td><input name="submit" type="submit"></td> <td><input name="reset" type="reset"></td> </tr> </table> </form> </body> </html>
index.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index page</title> </head> <body> index page <a href="admin.jsp">admin.jsp</a> </body> </html>
denied.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>denied page.</title> </head> <body> denied page. </body> </html>
admin.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>admin</title> </head> <body> hello, admin! </body> </html>
相关推荐
Spring安全研究 springSecurity框架学习源码
SpringSecurity 特点 和 Spring 无缝整合 全面的权限控制 专门为 Web 开发而设计 旧版本不能脱离 Web 环境使用 新版本中对整个框架进行了分层抽取,分成了核心模块和 Web 模块,单独引入核心模块就可以脱离 Web 环境...
Spring Boot 入门教程及示例代码,手把手快速上手教程-源码
springcloud-study:springcloud的学习
spring-boot-study:Spring引导学习
spring-source-study:Spring原始学习记录
Spring学习 这是一个用于学习的Spring项目。
SpringSecurity 常用获取登录用户//details里面可能存放了当前登录用户的详细信息,也可以通过cast后拿到。
Spring学习 ref:Younghan Kim的Infron Spring讲座
SpringInAction5Study SpringInAction5Study
Spring云侦探,rabbitmq,elasticsearch准备环境准备:工具版本或描述JDK 1.8 集成开发环境IntelliJ IDEA Maven3.x主机名配置:主机名配置(/ etc / hosts文件) 127.0.0.1发现配置服务器网关电影用户伪装功能区主机...
Spring CloudQuick Start高级使用Spring Cloud Config 集群使用注册中心能力即可,待续新版本集成dashboard stream有问题,后续补充熔断监控 - hystrix dashboard-Turbine新版本集成dashboard stream有问题,后续补充...
Spring学习 Spring学习守则。
Spring Cloud 为开发人员提供快速构建分布式系统的一些通用模式,其中包括:配置管理、服务发现、服务短路、智能路由、微型网关、控制总线、一次性令牌、全局锁、领导选举、分布式会话和集群状态。分布式系统间的...
Spring Security 学习 使用Spring Boot 2.0 为框架,学习Spring Security。 已完成功能: 完成基于数据库的用户登录验证。 完成基于数据库的权限简单权限验证(用户-角色-权限),使用自定义投票器实现鉴权。 实现...
java实现校园一卡通源码 Spring 代码及笔记参考自 Spring官方下载地址: Mybatis官方文档: Mybatis-spring官方文档: 简介 理念: 是现有的技术更加容易使用,整合了现有的技术框架。 导入maven库 <!-- ...
spring原始学习 Spring原始码 主要分析Spring IOC,DI,AOP和MVC模块源码,相关模块时序图见时序图文件夹下html。 一,Spring Boot启动流程及IOC源码 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是简化...
Spring学习Springframework 研究项目
spring-cloud-study spring cloud 学习 - 曾阿水
春天的引导实例 :grinning_face_with_smiling_eyes:该仓库为Spring / SpringBoot / SpringCloud系列技术栈,涵盖实战教程+原始码深度学习,一网打尽!... spring-boot-config-study: spring-boot-swagger-demo: