SSM集成SpringSecurity(一)准备环境

释放双眼,带上耳机,听听看~!

前言

本系列文章主要是借助ssm环境整合springSecurity并学习,由于本系列文章是有关联性的,所以有可能的话请从第一章(本篇)开始阅读。项目的全量代码:https://github.com/tomorrowWithYou/ssm_security.git

简介

SpringSecurity是Spring的全家桶之一,功能十分强大,主要是提供web安全方面的保护,涉及到系统的认证,授权,防止跨站攻击等。

本系列文章会结合实例向大家说明如何使用SpringSecurity。

  • 搭建SSM并集成Security
  •  使用HttpBasic方式实现认证
  •  使用FormLogin方法实现认证
  •  自定义登录页面,自定义登录请求
  •  user-service配置实现用户权限访问控制
  •  自定义登录成功失败的处理
  •  基于RBAC实现菜单权限
  •  自定义UserDetailService类实现用户权限访问控制
  •  密码
  •  自定义过滤器实现验证码校验
  •  rememberMe
  •  使用标签优化权限细节。
  •  获取登录的用户信息

搭建SSM并集成Security

1: 创建maven项目

2: 导入坐标依赖

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">

 

<modelVersion>4.0.0</modelVersion>

 

<artifactId>ssm_security</artifactId>

<packaging>war</packaging>

 

<properties>

<jdk.version>1.8</jdk.version>

<spring.version>4.3.10.RELEASE</spring.version>

<spring.security.version>4.2.3.RELEASE</spring.security.version>

<jstl.version>1.2</jstl.version>

<servlet.version>2.5</servlet.version>

</properties>

 

<dependencies>

 

<!– Spring dependencies –>
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

 

<!– Spring Security –>
<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-web</artifactId>

<version>${spring.security.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-config</artifactId>

<version>${spring.security.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-taglibs</artifactId>

<version>${spring.security.version}</version>

</dependency>

 

<!– jstl for jsp page –>
<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>${jstl.version}</version>

</dependency>
<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>${servlet.version}</version>

<scope>provided</scope>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>
<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.5</version>

</dependency>

 

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.4.4</version>

</dependency>

 

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

 

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.3.0</version>

</dependency>

 

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.1.7</version>

</dependency>

 

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.41</version>

</dependency>

 

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

 

</dependencies>

 

<build>
<plugins>
<!– jdk版本插件 –>
<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.2</version>

<configuration>
<source>1.8</source>

<target>1.8</target>

<encoding>UTF-8</encoding>

<showWarnings>true</showWarnings>

</configuration>
</plugin>

 

<!– tomcat7插件 –>
<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<version>2.1</version>

<configuration>

<port>8080</port>

<path>/ss2</path>

<server>tomcat7</server>

</configuration>
</plugin>
</plugins>
</build>
</project>

3: 新增spring相关的配置文件

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"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

 

<!– SpringSecurity过滤器链 –>
<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 –>
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>
<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:applicationContext.xml

classpath:spring-security.xml

</param-value>
</context-param>

 

<!–启动SpringMVC–>
<servlet>

<servlet-name>DispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>
<!– 服务器启动加载Servlet–>

<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>

<servlet-name>DispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>
</web-app>

 

springmvc.xml的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:contenxt="http://www.springframework.org/schema/context"

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/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

 

</beans>

 

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"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

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-4.2.xsd

http://www.springframework.org/schema/security

http://www.springframework.org/schema/security/spring-security-4.2.xsd">

 

security:http

security:http-basic/

</security:http>

 

security:authentication-manager

</security:authentication-manager>

</beans>
 

 

4:启动项目,能成功访问项目,则配置完毕,可以使用springSecurity的。到目前为止,没有配置mybatis相关的东西,只是引入了依赖。后面使用的时候会把mybatis整合进来。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全资讯

杭州骑呗单车福利:2017年全民免费骑

2016-12-30 15:58:26

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索