初识Spring Security框架

内容介绍     

        Spring Security 提供了基于javaEE的企业应用软件全面的安全服务。这里特别强调支持使用SPring框架构件的项目,Spring框架是企业软件开发javaEE方案的领导者。如果你还没有使用Spring来开发企业应用程序,我们热忱的鼓励你仔细的看一看。熟悉Spring特别是注入原理帮助你更快更方便的使用Spring Security。

       人们使用Spring Secruity的原因有很多,单大部分都发现了javaEE的Servlet规范或EJB规范中的安全功能缺乏典型企业应用场景所需的深度。提到这些规范,重要的是要认识到他们在WAR或EAR级别无法移植。因此如果你更换服务器环境,这里有典型的大量工作去重新配置你的应用程序员安全到新的目标环境。使用Spring Security 解决了这些问题,也为你提供许多其他有用的,可定制的安全功能。

        正如你可能知道的两个应用程序的两个主要区域是“认证”和“授权”(或者访问控制)。这两个主要区域是Spring Security 的两个目 标。“认证”,是建立一个他声明的主题的过程(一个“主体”一般是指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。“授权”指确定一个主体是否允许在你的应用程序执行一个动作的过程。为了抵达需要授权的店,主体的身份已经有认证过程建立。这个概念是通用的而不只在Spring Security中。

入门Demo

1、开发步骤

① 新建maven project工程。

② 配置web.xml

<context-param>
	<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security.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>

③ 新建html页面

登录成功的html界面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
欢迎进入神奇的spring security世界~~~
</body>
</html>

登录失败的html界面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
欢迎进入神奇的spring security世界~~~
</body>
</html>

登录的界面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>

--欢迎的登陆我的系统--
<form action="/login" method="post">
	用户名:<input name="username"><br>
	密码:<input name="password"><br>
	<button>登陆</button>
</form>


</body>
</html>

④ 配置spring-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.xsd
						http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

	<!-- 设置页面不登陆也可以访问 -->
	<http pattern="/login.html" security="none"></http>
	<http pattern="/login_error.html" security="none"></http>

	<!-- 页面的拦截规则    use-expressions:是否启动SPEL表达式 默认是true -->
	<http use-expressions="false">
		<!-- 当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
		<intercept-url pattern="/**" access="ROLE_USER"/>
		<!-- 开启表单登陆功能 -->
		<form-login   login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
		<csrf disabled="true"/>
	</http>
	
	<!-- 认证管理器 -->
	<authentication-manager>
		<authentication-provider>
			<user-service>
				<user name="admin" password="123456" authorities="ROLE_USER"/>
			</user-service>
		</authentication-provider>	
	</authentication-manager>
		
</beans:beans>

⑤ 打开浏览器进行访问http://localhost:9090/index.html

    

   输入错误的用户名和密码,点击登录

   

    输入正确的用户名和密码,点击登陆

    

福利

  1. 超链接的标签如何提交form表单?(最便捷的方式)         答:利用 onclick="document:form-login.submit()"提交(form-login就是form表单的id属性值)
  2. spring security拦截了ifream,影响了页面的呈现,如何快速解决呢?     答:spring-security.xml配置文件加入下面的配置:
<headers>
	<frame-options policy="SAMEORIGIN"/>
</headers>

猜你喜欢

转载自blog.csdn.net/javashareauthor/article/details/81670992