SpringSecurity学习记录__HelloWorld案例__01

Spring Security 简介

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。

 一个能够为基于Spring的企业应用系统提供声明式的安全訪问控制解决方式的安全框架(简单说是对访问权限进行控制),应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。   spring security的主要核心功能为 认证和授权,框架也是基于这两个核心功能去实现的。

本文演示了Spring Security的最最基本用法

步骤1: 工程目录结构

SpringSecurity学习记录__HelloWorld案例__01_第1张图片

 第2步:创建Maven工程更新pom.xml,添加所需依懒



	4.0.0
	com.study
	SpringSecurity-HelloWorld-XML
	SpringSecurity-HelloWorld-XML
	war
	1.0
	
		1.6
		3.2.8.RELEASE
		3.2.3.RELEASE
		1.2
	

	
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		
		
			org.springframework
			spring-beans
			${spring.version}
		
		
			org.springframework
			spring-expression
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		

		
			org.springframework
			spring-webmvc
			${spring.version}
		

		
		
			org.springframework.security
			spring-security-core
			${spring.security.version}
		

		
			org.springframework.security
			spring-security-web
			${spring.security.version}
		

		
			org.springframework.security
			spring-security-config
			${spring.security.version}
		

		
		
			jstl
			jstl
			${jstl.version}
		

	
	
		
			
				maven-eclipse-plugin
				2.9
				
					
						org.springframework.ide.eclipse.core.springnature
					
					
						org.springframework.ide.eclipse.core.springbuilder
					
					true
					true
				
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.5.1
				
					1.6
					1.6
					-Xlint:all
					true
					true
				
			
			
				org.codehaus.mojo
				exec-maven-plugin
				1.2.1
				
					org.test.int1.Main
				
			
		
	

 主要还是在springweb开发所需jar上添加三个Spring Security所需jar:

SpringSecurity学习记录__HelloWorld案例__01_第2张图片

相关配置文件:

web.xml




	
	
		contextConfigLocation
		/WEB-INF/spring/root-context.xml
	

	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		appServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/spring/appServlet/servlet-context.xml
		
		1
	

	
		appServlet
		/
	

	
	
		springSecurityFilterChain
		org.springframework.web.filter.DelegatingFilterProxy
	

	
		springSecurityFilterChain
		/*
	


root-context.xml




	
	
	

此配置其实包含了“配置模块化”的思想,通过import,把跟Security相关的配置,单独放在另一个xml文件中,然后import进来,配置文件特别多的时候,这样可以使Spring的配置看上去更有条理

spring-security.xml



	
		
		
	

	
		
			
				
			
		
	

这才是Security的精华所在,8-11行,表示“/admin”请求需要ROLE_USER角色的用户才能访问,13-19行配置了一个用户dk以及密码123456,并将该用户授于ROLE_USER角色(当然:这里只是演示,实际应用中,更常见的做法是将用户名、密码放到数据库中) 

servlet-context.xml




	
	
	
	

	
	

	
	
		
		
	
	
	
	
	
	

主要用来处理Spring-MVC的相关内容,跟Security其实没啥关系

 第3步:前端视图页面部分

通用页面(所以角色都可以访问)

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="false"%>





${title}


	

Title:${title}

Message:${message}

特定页面(不是所以角色都可以访问)

admin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="true"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




${title}


	

Title : ${title}

Message : ${message}

Welcome : ${pageContext.request.userPrincipal.name} | "> Logout

 第4步 :后端JAVA部分

HelloController.java

对应前端两个页面的请求控制

package com.csdn.dk;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
	public ModelAndView welcome() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Welcome - Spring Security Hello World");
		model.addObject("message", "This is welcome page!");
		model.setViewName("hello");
		return model;

	}

	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public ModelAndView admin() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Admin - Spring Security Hello World");
		model.addObject("message", "This is protected page!");
		model.setViewName("admin");

		return model;

	}

}

 第5步 :添加javaee支持(也可以单独添加javaee.jar支持)

SpringSecurity学习记录__HelloWorld案例__01_第3张图片

加载项目到Tomcat后运行起来,录入登录用户名与密码进行测试

SpringSecurity学习记录__HelloWorld案例__01_第4张图片

 登录成功页面的截图:

SpringSecurity学习记录__HelloWorld案例__01_第5张图片

登录失败页面的截图: 

SpringSecurity学习记录__HelloWorld案例__01_第6张图片

在上面的案例中,有一些框架默认的配置:

  1.  admin.jsp中退出的a链接: j_spring_security_logout,是Spring Security默认生成的logout地址,除非开发人员有其它设置,否则默认退出地址就是它
  2. 还有就是我们访问/admin时,会重定向到Spring Security自动生成的login页面 spring_security_login,也就是没有编写登录表单form,系统提示一个默认的登录表单。

SpringSecurity学习记录__HelloWorld案例__01_第7张图片

这种默认约定配置,在springBoot中体现的最明显,也就是现在流行的“约定大于配置”的思想。

你可能感兴趣的:(spring,系统安全技术)