近日学习Spring Security框架,学习到利用安全框架完成系统的安全通道控制时,来来回回遇到了不少问题。spring教程上写的略简单,对于我等小白来讲不足以支撑看书编码,好在网络上有资料可以查询,在吸取了他人经验,再结合自身的调试,最终实现了想要的效果。接下来,我就一步一步还原这个实现的过程,请往下看。

   一、关于Tomcat的证书安装,ssl监听端口的实现说明

         使用Tomcat启用ssl,需要在server.xml文件中 添加ssl请求的监听设置。方式有多种,这里提供一种,不是重点,不做赘述。

1.使用jdk的keytool工具,生成服务端证书

keytool -genkeypair -alias tomcat -keyalg RSA -keypass 123456 -storepass 123456 -keystore E:/tomcat.keystore

          2.配置server.xml的ssl监听

    <Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" minSpareThreads="5" maxSpareThreads="75"
enableLookups="true" disableUploadTimeout="true"
acceptCount="100" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="E:/tomcat.keystore"
keystorePass="123456"/>

             

  3.(可以选择)直接配置web.xml,完成安全通道的拦截开启。这种方式不需要spring security框架。

<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>

   二、验证Spring Security 安全通道设置的实现说明

 1、参考spring教程说明,完成第一次的启用https的尝试。对 /free/** 的请求开启安全连接。

        

  • 我的设置代码 ( .and().requiresChannel().antMatchers("/free/**").requiresSecure()
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* HTTP请求处理
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
.and().authorizeRequests().antMatchers("/**/*.do").authenticated()
.and().requiresChannel().antMatchers("/free/**").requiresSecure()
//.channelProcessors(getChannelProcessors())
.and().csrf().disable(); //暂时禁用CSRF
}
  • 测试一下。报出了一堆filter执行的错误,并且将请求的路径也改了,多了一级applicationContext。

  • 跟踪源码,查看一下错误原因。堆栈调用过程这里就不细讲了,这里只说一下问题根源。

如图1,调试发现需要启动安全访问的请求都会进入这个方法,组装重定向地址。redirectPort 应该返回https请求监听端口,但是很遗憾的是这个值是null。

如图2,接下来我看了下 getMappedPort这个方法,发现Spring Security默认是内置个两组对应的映射端口(80->443,8080->8443)。到这里上面出错就好理解了,我测试用的tomcat,设置的http请求监听端口是8898,根本就找不到对应的https端口。知道了问题,接下来就开始整改吧。

  2、个人源码分析,暴力指定自己的通道请求处理,设置channelProcessors。

  • 我的整改代码
    /**
* HTTP请求处理
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
.and().authorizeRequests().antMatchers("/**/*.do").authenticated()
.and().requiresChannel().channelProcessors(getChannelProcessors())
.antMatchers("/free/**").requiresSecure()
.and().csrf().disable(); //暂时禁用CSRF
}
/**
* 设置自己的通道处理器
* @return
*/
private List<ChannelProcessor> getChannelProcessors(){
List<ChannelProcessor> list = new ArrayList<ChannelProcessor>();
SecureChannelProcessor processor = new SecureChannelProcessor(); RetryWithHttpsEntryPoint entryPoint = ((RetryWithHttpsEntryPoint)processor.getEntryPoint());
//重新定义port映射
PortMapperImpl portMapper = new PortMapperImpl();
HashMap<String,String> maper = new HashMap<String,String>();
maper.put("80","443");
maper.put("8080","8443");
maper.put("8898","8443");
portMapper.setPortMappings(maper);
entryPoint.setPortMapper(portMapper);
list.add(processor);
list.add(new InsecureChannelProcessor());
return list;
}
  • 测试一下,看看结果。不出所料,已经可以了!

  3.、回头想想,框架不会这么烂吧?不可能一个端口映射,还得自己分析一堆源码才知道怎么玩?会不会是我自己没找到门路?答案是肯定的,实际上框架真的已经提供了配置端口映射的方法。接下来就是优雅的第3版实现,请往下看。

  • 我的整改代码
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* HTTP请求处理
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
.and().portMapper().http(8898).mapsTo(8443) //添加端口映射,做测试用
.and().authorizeRequests().antMatchers("/**/*.do").authenticated()
.and().requiresChannel().antMatchers("/free/**").requiresSecure()
.and().requiresChannel().anyRequest().requiresInsecure()
.and().httpBasic()

.and().csrf().disable(); //暂时禁用CSRF
}
  • 测试一下,看看结果。非常不错,这才是正确的道路!

  

4、试了几把跳转,发现点击退出系统按钮,退回到登录页面也成了https请求,不符合我想要的设置效果啊。按这个测试结果来看,猜测整个过程应该是这样的。当我们成功进入一次https请求后,之后的请求因为都是指定的相对路径,所以全部指向了8443端口。需要有个显示的设置,让其他请求被http架构处理。接下来是我的第4版实现,请往下看。

  • 我的整改代码
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* HTTP请求处理
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
String doUrl = "/**/*.do";
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
.and().portMapper().http(8898).mapsTo(8443) //添加端口映射,做测试用
.and().authorizeRequests().antMatchers(doUrl).authenticated()
.and().requiresChannel().antMatchers("/free/**",doUrl).requiresSecure()
.and().requiresChannel().antMatchers(doUrl).requiresInsecure()
.and().httpBasic()
.and().csrf().disable(); //启用CSRF
}
  • 测试一下,看看结果。可以了,现在可以做到只对/free/路径下的请求开启https安全通道了!

  

  

至此开启安全访问通道的功能实现就完成了。希望对读到结尾的你有所帮助!如果有好的意见,欢迎评论交流。

Tomcat8+Spring-Security 启用安全通道(https)的一步步实现的更多相关文章

  1. Spring Security权限控制

    Spring Security官网 : https://projects.spring.io/spring-security/ Spring Security简介: Spring Security是一 ...

  2. 使用Spring Security控制会话

    1.概述 在本文中,我们将说明Spring Security如何允许我们控制HTTP会话.此控件的范围从会话超时到启用并发会话和其他高级安全配置. 2.会话何时创建? 我们可以准确控制会话何时创建以及 ...

  3. 结合Spring Security进行web应用会话安全管理

    在本文中,将为大家说明如何结合Spring Security 和Spring Session管理web应用的会话. 一.Spring Security创建使用session的方法 Spring Sec ...

  4. spring security控制session

    spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...

  5. SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑

    前言 最近在做项目的时候,基于前后端分离的权限管理系统,后台使用 Spring Security 作为权限控制管理, 然后在前端接口访问时候涉及到跨域,但我怎么配置跨域也没有生效,这里有一个坑,在使用 ...

  6. Spring Security 源码分析(四):Spring Social实现微信社交登录

    社交登录又称作社会化登录(Social Login),是指网站的用户可以使用腾讯QQ.人人网.开心网.新浪微博.搜狐微博.腾讯微博.淘宝.豆瓣.MSN.Google等社会化媒体账号登录该网站. 前言 ...

  7. spring security oauth2 client_credentials模

    spring security oauth2 client_credentials模 https://www.jianshu.com/p/1c3eea71410e 序 本文主要简单介绍一下spring ...

  8. 关于 Spring Security OAuth2 中 CORS 跨域问题

    CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了 AJA ...

  9. spring security 学习文档

    web service Prepared by:   Sea                                                                       ...

随机推荐

  1. CSS基础--常用样式

    一.背景相关 背景颜色 background-color :颜色名称/rgb值/十六进制值 背景图片 background-image :url('') 背景图片平铺方式 background-rep ...

  2. linux_sudo命令

    sudo 为了收拾su命令的烂摊子 普通用户要切换root,必须要知道root密码,那么相当于人人都有了核按钮,那是绝对不允许的 用su切换到root,无法对是谁要求root权限的身份进行控制,拿到r ...

  3. junit源码解析总结

    前面的博客我们也已经整理到了,我们使用junit38,在写测试类的时候我们的测试类必须继承TestCase.这个所有测试类的父类在junit.framework包下面. 前面我们的整理都是说直接在ID ...

  4. keytool 错误:java.to.FileNotFoundException:

    老是报如题的错误: 后来才知道是因为当前的目录下没有写的权限,所以需要指定一个路径来存放android.key: keytool -genkey -alias android.key -keyalg ...

  5. js事件绑定函数

    js中事件绑定方法大致有三种: 1.在DOM元素中绑定 <input onclick="alert('在DOM中绑定')" type="button" v ...

  6. python-day2数据类型

    内容介绍 数据类型 字符编码 文件处理 1.什么是数据? x=10 , 10是我们要存储的数据. 2.为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类 ...

  7. 探索ArrayList自动改变size真相

    探索ArrayList自动改变size真相 ArrayList的列表对象实质上是存储在一个引用型数组里的,有人认为该数组有“自动增长机制”可以自动改变size大小.正式地说,该数组是无法改变 大小的, ...

  8. 【视频编解码·学习笔记】4. H.264的码流封装格式

    一.码流封装格式简单介绍: H.264的语法元素进行编码后,生成的输出数据都封装为NAL Unit进行传递,多个NAL Unit的数据组合在一起形成总的输出码流.对于不同的应用场景,NAL规定了一种通 ...

  9. ASP.NET Core 2.0 : 一. 概述

    为什么要使用 ASP.NET Core? .NET Core 刚发布的时候根据介绍就有点心里痒痒, 大概看了一下没敢付诸于行动,  现在2.0发布了一段时间了, 之前对其"不稳定" ...

  10. 02_Linux图形界面及文件系统结构介绍

    一.根目录 /       : 它跟Windows的C.D.E.F不同,在Linux中,所有的文件系统(光驱.U盘.硬盘)都挂载到根目录的某一个文件夹下 bin   : 存放二进制可执行文件 sbin ...