CAS 之 Https And Database Authentication(三)

标签(空格分隔): CAS

sso-examples-guides源码


Intro(介绍)

由上节可知Apereo CAS 官方默认使用 https 的方式进行部署:

为了安全,我们使用https方式,并禁用静态账户信息。

What you’ll need(需要掌握)

  • JDK 1.8 or later
  • Maven 3.2+
  • spring boot
  • Spring Tool Suite (STS)
  • IntelliJ IDEA
  • keytool

生成证书

生成key

keytool -genkey -alias ssokeystore  -keyalg RSA -keysize 2048 -keypass 123456 -storepass 123456 -keystore D:/sso/sso.keystore -dname "CN=www.galsang.org,OU=galsang.org,O=galsang,L=Shanghai,ST=Shanghai,C=CN" -ext "san=dns:www.galsang.org,ip:192.168.6.53"

导出证书

keytool -export -file D:/sso/ssokeystore.crt -alias ssokeystore  -keystore D:/sso/sso.keystore  -keypass 123456 -storepass 123456
# 或
keytool -exportcert -alias ssokeystore -keystore D:/sso/sso.keystore -file D:/sso/ssokeystore.crt -keypass 123456 -storepass 123456

导入证书到本地JDK(客户端认证)

keytool -import -alias ssokeystore   -keystore D:/javaspace/Java/jdk1.8.0_131/jre/lib/security/cacerts -file D:/sso/ssokeystore.crt -keypass changeit -storepass changeit

删除证书

如果之前导入过该别名ssokeystore的证书,则删除证书

keytool -delete -alias ssokeystore   -keystore D:/javaspace/Java/jdk1.8.0_131/jre/lib/security/cacerts -keypass changeit -storepass changeit

查看密钥库证书

keytool -list  -keystore D:/javaspace/Java/jdk1.8.0_131/jre/lib/security/cacerts -keypass changeit -storepass changeit

查看指定证书内容

keytool  -printcert  -file "D:/sso/ssokeystore.crt "

https 配置

步骤一:将生成的密钥 sso.keystore 拷贝至 src/main/resources 目录下

步骤二:进行配置

由于5.2.0版本是默认是开启的,这里先关闭,后期上生产之前再开启并进行配置:

Ticket Granting Cookie

cas:
tgc:
secure: false # cas.tgc.secure=true

步骤三:进行application.yml配置

笔者喜欢使用yml文件的方式进行配置,故将application.properties文件中的配置迁移至application.yml,但依然要保留application.properties文件将原始war中的application.properties文件覆盖,这是因为 maven-war-plugin/overlays的缘故。


spring:
application:
name: cas-server
http:
encoding:
enabled: true
charset: UTF-8
force: true
thymeleaf:
encoding: UTF-8
cache: true
mode: HTML
aop:
auto: true
proxy-target-class: true # CAS Server Context Configuration
server:
context-path: /cas
port: 8443
max-http-header-size: 2097152
use-forward-headers: true
connection-timeout: 20000
error:
include-stacktrace: ALWAYS
compression:
enabled: true
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain
ssl:
key-store: classpath:sso.keystore
key-store-password: 123456
key-password: 123456
enabled: true
tomcat:
max-http-post-size: 2097152
basedir: build/tomcat
max-threads: 10
port-header: X-Forwarded-Port
protocol-header: X-Forwarded-Proto
protocol-header-https-value: https
remote-ip-header: X-FORWARDED-FOR
uri-encoding: UTF-8
accesslog:
enabled: true
pattern: "%t %a '%r' %s (%D ms)"
suffix: .log
session:
timeout: 300
cookie:
http-only: true
tracking-modes: COOKIE
context-parameters:
isLog4jAutoInitializationDisabled: true cas:
server:
name: https://www.galsang.org:8443
prefix: https://www.galsang.org:8443/cas
adminPagesSecurity:
ip: 127.0.0.1
authn:
accept:
users: casuser::Mellon,admin::adminto # 静态用户信息
# webflow:
# crypto:
# enabled: false #cas.webflow.crypto.enabled=false
tgc:
secure: false # cas.tgc.secure=true management:
security:
enabled: true
roles: ACTUATOR,ADMIN
sessions: if_required
context-path: /status
add-application-context-header: false security:
basic:
enabled: false
authorize-mode: role
path: /cas/status/** endpoints:
enabled: false
sensitive: true
restart:
enabled: false
shutdown:
enabled: false logging:
config: classpath:log4j2.xml info:
description: cas-server

Run(运行)

进入cas-server模块执行 build run 命令。

sso-examples-guides\cas-server>build run

访问入口: https://127.0.0.1:8443/cas/login

默认的静态账户信息, 账号:casuser, 密码: Mellon



使用我自定义的静态账户信息, 账号:admin, 密码: adminto

至此,系统可以使用https的形式进行访问,那么下面我们来看一下 Database Authentication是如何配置实现的。


Database Authentication 配置

步骤一: 禁用静态账户

# 禁止静态认证
staticAuthentication: false
# 将将静态账户信息置空
cas:
authn:
accept:
users:

步骤二: 设计数据库

数据库脚本如下:

DROP DATABASE IF EXISTS `cas_dev`;

CREATE DATABASE `cas_dev` character Set UTF8;

use `cas_dev`;

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `cas_user`
-- ----------------------------
DROP TABLE IF EXISTS `cas_user`;
CREATE TABLE `cas_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of cas_user
-- ----------------------------
INSERT INTO `cas_user` VALUES ('1', 'admin', '1e1e262780021c6844af137175b56804');

步骤三: pom.xml文件里增加依赖

        <!--引入数据库认证相关 start-->
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<!--引入数据库认证相关 end-->

步骤四: 在application.yml中增加相关配置。

最终application.yml内容为:


spring:
application:
name: cas-server
http:
encoding:
enabled: true
charset: UTF-8
force: true
thymeleaf:
encoding: UTF-8
cache: true
mode: HTML
aop:
auto: true
proxy-target-class: true # CAS Server Context Configuration
server:
context-path: /cas
port: 8443
max-http-header-size: 2097152
use-forward-headers: true
connection-timeout: 20000
error:
include-stacktrace: ALWAYS
compression:
enabled: true
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain
ssl:
key-store: classpath:sso.keystore
key-store-password: 123456
key-password: 123456
enabled: true
tomcat:
max-http-post-size: 2097152
basedir: build/tomcat
max-threads: 10
port-header: X-Forwarded-Port
protocol-header: X-Forwarded-Proto
protocol-header-https-value: https
remote-ip-header: X-FORWARDED-FOR
uri-encoding: UTF-8
accesslog:
enabled: true
pattern: "%t %a '%r' %s (%D ms)"
suffix: .log
session:
timeout: 300
cookie:
http-only: true
tracking-modes: COOKIE
context-parameters:
isLog4jAutoInitializationDisabled: true cas:
server:
name: https://www.galsang.org:8443
prefix: https://www.galsang.org:8443/cas
adminPagesSecurity:
ip: 127.0.0.1
tgc:
secure: false # cas.tgc.secure=true
authn:
accept:
users: # 静态用户信息 casuser::Mellon,admin::adminto
jdbc:
query[0]:
sql: select * from cas_user where username=?
healthQuery: select 1
isolateInternalQueries: false
# 指定时区 serverTimezone=Asia/Shanghai
url: jdbc:mysql://127.0.0.1:3306/cas_dev?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
failFast: true
isolationLevelName: ISOLATION_READ_COMMITTED
dialect: org.hibernate.dialect.MySQLDialect
leakThreshold: 10
propagationBehaviorName: PROPAGATION_REQUIRED
batchSize: 1
user: root
password: adminto
autocommit: false
maxAgeDays: 180
driverClass: com.mysql.cj.jdbc.Driver
idleTimeout: 5000
fieldPassword: password
passwordEncoder:
type: DEFAULT
characterEncoding: UTF-8
encodingAlgorithm: MD5 staticAuthentication: false management:
security:
enabled: true
roles: ACTUATOR,ADMIN
sessions: if_required
context-path: /status
add-application-context-header: false security:
basic:
enabled: false
authorize-mode: role
path: /cas/status/** endpoints:
enabled: false
sensitive: true
restart:
enabled: false
shutdown:
enabled: false logging:
config: classpath:log4j2.xml info:
description: cas-server

Run(运行)

进入cas-server模块执行 build run 命令。

sso-examples-guides\cas-server>build run

访问入口: https://127.0.0.1:8443/cas/login

使用数据库中默认的账户信息, 账号:admin, 密码: adminto

密码修改可以在src/test/java 目录下的 PasswordByMD5Main类进行重置密码,并更新到数据库即可。

至此,系统可以使用https的形式进行访问,并通过 Database Authentication进行用户认证。

Conclusions(结论)

  • Database Authentication 使用的是JPA方式,数据库连接池使用的是HikariCP
  • JPA是默认的 Database Authentication 方式,那么在后面的文章中将说明如何替换JPA

Recommendations(建议)

  • 使用https
  • 禁用静态账户
  • 工程复杂,一定要注意版本之间的关系,开源项目,最好对照源码编译版本进行部署。

原创声明

作者:随风浮云

出处:http://www.cnblogs.com/ljmatlight

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明。

文中有不妥或者错误的地方,欢迎勘误,如果你有更好的建议,可以给我留言讨论,共同进步。

互联网技术时效性较强,引用请慎重。


CAS 之 Https And Database Authentication(三)的更多相关文章

  1. cas单点登录-jdbc认证(三)

    前言 本节的内容为JDBC认证,查找数据库进行验证,其中包括: 密码加密策略(无密码,简单加密,加盐处理) 认证策略(jdbc) 一.业务需求 不同的公司,需求业务需求或者架构不一样导致我们实现验证的 ...

  2. 在阿里云 ECS 搭建 nginx https nodejs 环境(三、nodejs)

    在阿里云 ECS 搭建 nginx https nodejs 环境(三.nodejs) 在阿里云 ECS 搭建 nginx https nodejs 环境(三.nodejs) 第一步 指定版本源 执行 ...

  3. ORA-38760: This database instance failed to turn on flashback database 第三篇

    ORA-38760: This database instance failed to turn on flashback database  第三篇 第一篇 第二篇 问题现象:      在数据库a ...

  4. CAS进行https到http的改造方案,结合cookie源码分析

    先说具体的改造方案: 服务端: 一.CAS Server端的修改 1.找到cas\WEB-INF\deployerConfigContext.xml 对以下Bean增加参数p:requireSecur ...

  5. CAS去掉HTTPS认证

    如何去掉HTTPS认证? 说明:默认情况下HTTP也是可以访问CAS SERVER的,但认证,登陆,退出等操作均没有任何的效果.所以必须作出下面的修改 1.进入WEB-INF\spring-confi ...

  6. HTTPS那些事(三)攻击实例与防御(转载)

    原创地址:http://www.guokr.com/blog/148613/   在<HTTPS那些事(二)SSL证书>我描述了使用SSL证书时一些需要注意的安全问题,在这一篇文章里面我再 ...

  7. HTTPS和TCP协议三次握手设计

    1. 我们的TCP 三次握手大概是长这样 2.那么为什么 TCP 要采取三次握手,而不是两次或其他 首先我们要知道握手的目的: 为了保证通讯双方建立的连接是可靠的. 同时,为了保证性能,握手的次数要求 ...

  8. https验证证书的三个级别

    一.无条件信任证书 1. func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge ...

  9. HTTPS那些事(三)攻击实例与防御

    在<HTTPS那些事(二)SSL证书>我描述了使用SSL证书时一些需要注意的安全问题,在这一篇文章里面我再演示一下针对HTTPS攻击的一些实例,通过这些实例能更安全的使用HTTPS.知己知 ...

随机推荐

  1. 事件CEvent的使用 .

    CEvent类的一个对象,表示一个"事件"--一个允许一个事件发生时线程通知另一个线程的同步对象.在一个线程需要了解何时执行任务时,事件是十分有用的.例如,拷贝数据到数据文档时,线 ...

  2. Alibaba阿里巴巴开源软件列表

    整理和分享我大阿里的开源项目的相关网址: Git Hub上的开源软件网址: 1.https://github.com/alibaba 阿里巴巴开源技术汇总:115个软件 2.https://yq.al ...

  3. FusionCharts 2D柱状图和折线图的组合图

    1.设计思路 (1)了解组合图的特性以及用法,选用图的类型: (2)设计出两根柱子和两根折线,分开展示. 2.设计步骤 (1)设计页面 Column2DLine.html: <!DOCTYPE ...

  4. IE浏览器因缓存问题未能成功向后端发送请求的几个解决办法

    这段时间前后端联调,解决IE.谷歌等浏览器兼容问题,真是让人有点焦头烂额,各种奇葩问题层出不穷,旧问题刚去,新麻烦又来,其中尤其以IE浏览器缓存的问题最多.有若干次都是因为这个缓存,使得前端的请求没有 ...

  5. 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法

    在我编程中,我遇到了一个这样的报错, 可是我引用的product类中又确实定义了这么一个方法, protected void BindPageData(int categoryID) { Produc ...

  6. JAVA简单的网格布局管理器--JAVA基础

    网格布局管理器: GridLayoutDemo.java: import java.awt.GridLayout;import javax.swing.JButton;import javax.swi ...

  7. 【CJOJ1494】【洛谷2756】飞行员配对方案问题

    题面 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1 ...

  8. [BZOJ1085] [SCOI2005] 骑士精神 (A*)

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...

  9. Apache Shiro 标签方式授权

    Shiro提供了一套JSP标签库来实现页面级的授权控制. 在使用Shiro标签库前,首先需要在JSP引入shiro标签: <%@ taglib prefix="shiro"  ...

  10. DevExpress中GridControl自定义汇总列值(有选择性的汇总)

    今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...