您的位置:首页 > 其它

CAS之5.2x版本之jdbc配置多返回值-yellowcong

2018-03-14 11:35 495 查看
多属性返回,在单点登录系统中,必须做的。默认情况下,返回对的数据对象,只有一个username的信息,这很难满足业务的需求,我们除了可以自定义返回值的方式( CAS之5.2x版本自定义返回消息-yellowcong),还可以通过jdbc来配置多值返回的操作。但是这个肯定是没有自定义的方式灵活,但是满足基础的开发需求了。实现的步骤:1、配置services的 json文件,设置数据返回的规则。2、配置application.properties 设定

cas.serviceRegistry.initFromJson=true
从json初始化cas,3、配置数据库的连接和多属性信息(推荐数据库使用多表的方式,设计比较好)。4、配置pom.xml文件,让cas系统默认的json文件不打包,5、配置客户端(这个不是重点,没啥讲的)。

代码地址

https://gitee.com/yellowcong/springboot_cas/tree/master/cas-server-multi


目录结构

配置了json,同时配置



服务架构

地址服务
http://yellowcong.com:8888客户端
https://yellowcong.com:8443cas服务端

配置jdbc多属性返回值

1、配置services

yellowcong-1000.json,文件的命名是name+id.json,而且需要配置唯一的serviceId(服务匹配的正则),name(服务名称),id(唯一的id),evaluationOrder(匹配顺序,值越小,越先匹配),主题我设定的是
"theme":"apereo"
,这个是默认主题,attributeReleasePolicy

{
"@class": "org.apereo.cas.services.RegexRegisteredService",
"serviceId": "^(http|https|imaps)://yellowcong.*",
"name": "yellowcong",
"id": 1000,
"description": "yellowcong 用户网站的登录",
"evaluationOrder": 100,
"theme":"apereo",
"logoutUrl": "http://yellowcong.com:8888/user/loginOut/success" ,
"attributeReleasePolicy": {
"@class": "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
}
}


参数说明

参数意义
@classorg.apereo.cas.services.RegexRegisteredService(表示通过json注册服务)
serviceId匹配请求的网址 (唯一)
name服务名称
id编号, name+id.json 就是这个服务的文件名称了
description描述
evaluationOrder服务请求,匹配的顺序
themeapereo 这个是系统默认的主题
logoutUrl登出地址
attributeReleasePolicy返回值的策略:
1、Return All (所有配置返回的都返回)
2、Deny All (配置拒绝的出现则报错)
3、Return Allowed(只返回允许的主要属性)
4、自定义Filter(自定义过滤策略))

返回一部分数据

查询的数据信息中,我业务系统认为我只能返回这写字段里面的某几个字段,这时候,就可以用到这个了。

{
"@class" : "org.apereo.cas.services.RegexRegisteredService",
"serviceId" : "sample",
"name" : "sample",
"id" : 100,
"attributeReleasePolicy" : {
"@class" : "org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy",
"allowedAttributes" : [ "java.util.ArrayList", [ "cn", "mail", "sn" ] ]
}
}


2、让json配置的服务生效

我们配置了json的服务后 ,cas现在还是不认的,需要配置 application.properties,添加配置,设定从 json初始化json

#开启识别json文件,默认false
cas.serviceRegistry.initFromJson=true


3、配置数据库返回多属性

配置application.properties,设定数据库返回多属性,在这个地方,属性的返回,有分为多属性返回,和但属性返回的情况。用户表的用户信息,肯定是一条的,但是权限和角色信息,可能是多条了。

#-----------------------------数据库认证--------------------------------
cas.authn.jdbc.query[0].sql=SELECT * FROM sys_user WHERE username = ?
#select * from cms_auth_user where user_name=?
cas.authn.jdbc.query[0].healthQuery=
cas.authn.jdbc.query[0].isolateInternalQueries=false
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/yellowcong2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].leakThreshold=10
cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.jdbc.query[0].batchSize=1
cas.authn.jdbc.query[0].user=root
#cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
cas.authn.jdbc.query[0].password=root
cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].idleTimeout=5000
# cas.authn.jdbc.query[0].credentialCriteria=
# cas.authn.jdbc.query[0].name=
# cas.authn.jdbc.query[0].order=0
# cas.authn.jdbc.query[0].dataSourceName=
# cas.authn.jdbc.query[0].dataSourceProxy=false
#密码字段的信息
cas.authn.jdbc.query[0].fieldPassword=PASSWORD
#加密策略
cas.authn.jdbc.query[0].passwordEncoder.type=NONE

#配置多属性返回
#----------------------单列值情况----------------------------
#定义三个属性的单列
cas.authn.attributeRepository.jdbc[0].attributes.status=status
cas.authn.attributeRepository.jdbc[0].attributes.email=email
cas.authn.attributeRepository.jdbc[0].attributes.password=password
#设置数据为单行的情况
cas.authn.attributeRepository.jdbc[0].singleRow=true
cas.authn.attributeRepository.jdbc[0].order=0
cas.authn.attributeRepository.jdbc[0].url=${cas.authn.jdbc.query[0].url}
# 以下属性为查询sql时,当为多个时逗号分隔,如填写username、email,sql会变成 select * from sys_user where username=${?} {and/or} email=${?}
#cas.authn.attributeRepository.jdbc[0].username=username、email
#cas.authn.attributeRepository.jdbc[0].queryType=OR
#这个相当于查询的sql,设定查询的字段
#select * from yellowcong_users where username = ?
cas.authn.attributeRepository.jdbc[0].username=username
#多个属性的情况
cas.authn.attributeRepository.jdbc[0].user=${cas.authn.jdbc.query[0].user}
cas.authn.attributeRepository.jdbc[0].password=${cas.authn.jdbc.query[0].password}
#这个地方的{0} 就是  cas.authn.attributeRepository.jdbc[0].username  对于的查询条件
cas.authn.attributeRepository.jdbc[0].sql=select * from sys_user where {0}
cas.authn.attributeRepository.jdbc[0].dialect=${cas.authn.jdbc.query[0].dialect}
cas.authn.attributeRepository.jdbc[0].ddlAuto=none
cas.authn.attributeRepository.jdbc[0].driverClass=${cas.authn.jdbc.query[0].driverClass}
cas.authn.attributeRepository.jdbc[0].leakThreshold=10
cas.authn.attributeRepository.jdbc[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.attributeRepository.jdbc[0].batchSize=1
cas.authn.attributeRepository.jdbc[0].failFast=true
#----------------------单列值情况----------------------------

#----------------------多列值情况----------------------------
#多列值映射的情况,一个用户有多列值的情况
#多行(多个值的情况,我们需要做成key -val的形式,一个key 可以对应多个字段)
cas.authn.attributeRepository.jdbc[1].attributes.role=role_multi
cas.authn.attributeRepository.jdbc[1].attributes.acl=acl_multi
#键值对(指定 key 和 val的字段)
cas.authn.attributeRepository.jdbc[1].columnMappings.ATTR_KEY=ATTR_VAL
#多行
cas.authn.attributeRepository.jdbc[1].singleRow=false
cas.authn.attributeRepository.jdbc[1].order=1
#数据库地址
cas.authn.attributeRepository.jdbc[1].url=${cas.authn.jdbc.query[0].url}
#数据库查询的字段
cas.authn.attributeRepository.jdbc[1].username=username
#数据库连接 用户名
cas.authn.attributeRepository.jdbc[1].user=${cas.authn.jdbc.query[0].user}
#数据库连接 密码
cas.authn.attributeRepository.jdbc[1].password=${cas.authn.jdbc.query[0].password}
#查询多属性的sql, 和 刚刚的username 拼接
#select * from sys_attrs where username = ?
cas.authn.attributeRepository.jdbc[1].sql=select * from sys_attrs where {0}
cas.authn.attributeRepository.jdbc[1].dialect=${cas.authn.jdbc.query[0].dialect}
cas.authn.attributeRepository.jdbc[1].ddlAuto=none
cas.authn.attributeRepository.jdbc[1].driverClass=${cas.authn.jdbc.query[0].driverClass}
cas.authn.attributeRepository.jdbc[1].leakThreshold=10
cas.authn.attributeRepository.jdbc[1].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.attributeRepository.jdbc[1].batchSize=1
cas.authn.attributeRepository.jdbc[1].failFast=true
#----------------------多列值情况----------------------------


多属性问题

对于多属性中,用户有多个角色,多个权限的情况,我们需要将所有的多属性整合到一张表上,然后设定键值对,对于这种情况,解决的方案有三种:1、建表的时候,设定表的结构为这种key,val的方案(一般不要这种设计方法),2、多表的情况,通过创建视图的方式(推荐),3、查询的时候,sql语句的优化(这种就不需要考虑了,sql复杂话了)。



方案一(多表的配置)

这个地方,我创建了三张表,分别是系统的三张表,然后通过视图的方式,将 这三张表组成了键值对,让cas直接查询视图,这种方案推荐使用。

表名称意义
sys_role角色表
sys_acl控制权限
sys_user用户名
sys_attrs视图(用于将上面的三张表弄成键值对的形式)


-- 用户表
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`email` varchar(128) DEFAULT NULL COMMENT '邮箱|登录帐号',
`password` varchar(32) DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
`status` int(1) DEFAULT '1' COMMENT '1:有效,0:禁止登录',
`username` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

-- 权限表
CREATE TABLE `sys_acl` (
`username` varchar(32) DEFAULT NULL COMMENT '用户名',
`acl` varchar(32) DEFAULT NULL COMMENT '权限'
) ENGINE=InnoDB DEFAULT CHARSET=utf8

-- 角色表
CREATE TABLE `sys_role` (
`username` varchar(32) DEFAULT NULL COMMENT '用户名',
`role` varchar(16) DEFAULT '' COMMENT '角色'
) ENGINE=InnoDB DEFAULT CHARSET=utf8

-- 视图
CREATE VIEW sys_attrs AS
-- 查询出访问权限的信息
SELECT
a.`username` AS 'USERNAME',
'acl' AS 'ATTR_KEY' ,
b.`acl` AS 'ATTR_VAL'
FROM
sys_user a,
sys_acl b
WHERE a.`username` = b.`username`
UNION
-- 查询 角色的信息
SELECT
a.`username`  AS 'USERNAME',
-- 角色
'role' AS 'ATTR_KEY' ,
b.role AS 'ATTR_VAL'
FROM
sys_user a,
sys_role b
WHERE a.`username` = b.`username`

-- 表数据信息
-- 用户表信息
insert  into `sys_user`(`id`,`email`,`password`,`create_time`,`last_login_time`,`status`,`username`) values (1,'717350389@qq.com','yellowcong',NULL,NULL,1,'yellowcong');

-- 角色 表信息
insert  into `sys_role`(`username`,`role`) values ('yellowcong','管理员'),('yellowcong','老师');

-- 权限表信息
insert  into `sys_acl`(`username`,`acl`) values ('yellowcong','打酱油'),('yellowcong','java'),('yellowcong','都比权限');


方案2(键值对的表设计)

下面这种,是将用户的权限与 分组,用简单的表结构的方式,这也是方案1的配置,这种方式不推荐

-- 创建用户表
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`email` varchar(128) DEFAULT NULL COMMENT '邮箱|登录帐号',
`password` varchar(32) DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
`status` int(1) DEFAULT '1' COMMENT '1:有效,0:禁止登录',
`username` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

-- 插入用户信息
insert  into `sys_user`(`id`,`email`,`password`,`create_time`,`last_login_time`,`status`,`username`) values (2,'717350389@qq.com','yellowcong',NULL,NULL,1,'yellowcong');

-- 多属性表,可以说是权限表了
CREATE TABLE `sys_attrs` (
`USERNAME` varchar(30) NOT NULL,
`ATTR_KEY` varchar(50) NOT NULL,
`ATTR_VAL` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 插入多表的键值对信息
insert  into `sys_attrs`(`USERNAME`,`ATTR_KEY`,`ATTR_VAL`) values ('yellowcong','role','ADMIN_ROLE'),('yellowcong','role','MANAGEMENT_ROLE'),('yellowcong','role','DEV_ROLE'),('yellowcong','acl','doubi1'),('yellowcong','acl','doubi2');


4、配置pom.xml,取消cas默认json配置的服务

在原来打包的基础上,干掉json的文件的打包,这样系统默认的json配置就不会打包了

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>cas</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
<recompressZippedFiles>false</recompressZippedFiles>
<archive>
<compress>false</compress>
<manifestFile>${manifestFileToUse}</manifestFile>
</archive>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp${app.server}</artifactId>
</overlay>
</overlays>
<!-- 去掉默认的json配置文件 -->
<dependentWarExcludes>
**/services/*.json
</dependentWarExcludes>
</configuration>
</plugin>


启动服务

build.cmd run


测试结果

测试中,我们使用的是 yellowcong ,这个用户,然后获取到了yellowcong用户的组以及权限信息。



参考文章

http://blog.csdn.net/u010475041/article/details/78442603

https://apereo.github.io/cas/5.2.x/integration/Attribute-Release-Policies.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: