springboot实现CAS的server服务器端的搭建,并实现链接mysql数据库,自定义加密算法

   在实现单点登陆的过程中,我踩了很多坑,浏览了许多资料,终于将CAS的服务端搭建完成了,以下是我这两天搭建的心得,以及引用的一些大佬的资料,希望对大家有帮助。

一,下载源码

   首先是下载cas源码,git地址是:https://github.com/apereo/cas-overlay-template (此处注意,一定要选择5.3版本的源码。超过 5.3版本的cas取消了pom,导致很难进行打包,我的项目是使用MAVEN的springboot项目。而5.2版本的源码,在我这部署上去,访问登陆地址时,一直会报404的错误。因此使用5.3版本的。)

二,配置https,并部署到tomcat运行 

   一路下载完成后,由于cas要求要用https来进行访问,因此,我们首先要来生成一个https证书,并信任他,然后我们需要将cas源码打包成cas.war,放入到tomcat中进行运行验证。此处请参考大神

这个名字想了很久的文章 https://blog.csdn.net/qq_34021712/article/details/80871015  这篇文章只要看到将cas部署到tomcat,并且运行tomcat后能够进入登陆页面即可,overlay那块可以不看。(此处强力此大神的文章,他写的有关shiro的博客非常之详尽,基本上把shiro的所有知识点都整理好了。)

三,将cas源码部署到idea中

   在以上步骤完成后,我们已经有一个初步的cas服务端了,但是此时的cas只有默认的账号密码,而我们项目需要用的是应该是让他连接到我们的数据库中进行使用,我这里使用的是mysql数据库。因此需要进行以下操作。

   首先,我们将cas源码导入成MAVEN项目到idea中,导入后,在pom中加入以下jar包:

<dependency>
                    <groupId>org.apereo.cas</groupId>
                    <artifactId>cas-server-support-jdbc-drivers</artifactId>
                    <version>${cas.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apereo.cas</groupId>
                    <artifactId>cas-server-support-jdbc</artifactId>
                    <version>${cas.version}</version>
                </dependency>

                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.38</version>
                </dependency>

  然后,我们自己创建一个java源文件,用于之后编写自定义加密的java代码,然后将必要的文件如application.properties、META-INF和log4j2.xml从之前在tomcat跑的cas.war包里面拷贝出来,然后再idea里面配置tomcat,运行结果和之前在外部tomcat的结果一致即表示搭建成功啦。这个部分的具体操作详见文章https://blog.csdn.net/csolo/article/details/95597820 他里面对于这个项目目录的搭建讲得非常详细。

四,将cas项目连接mysql,并实现自定义加密

   搭建完项目框架后,重点来啦,就是如何将mysql连接进去(最后我会贴上我的application.properties的内容)。我们只需要打开application.properties,对其进行配置即可。首先配置ssl,这个是用来对https进行处理的,

#你的tomcat.keystore的路径
server.ssl.key-store=file:F:/tomcat.keystore
#你之前在配置https时配置的密码
server.ssl.key-store-password=asdasd
#同上
server.ssl.key-password=asdasd

然后,我们将默认的密码注销掉

#cas.authn.accept.users=casuser::Mellon

最后,我们配置关于mysql的配置

#Query Database Authentication 数据库查询校验用户名开始
#查询账号密码sql,必须包含密码字段
cas.authn.jdbc.query[0].sql=select password from sys_user where username=?
#指定上面的sql查询字段名(必须)
cas.authn.jdbc.query[0].fieldPassword=password
#指定过期字段,1为过期,若过期不可用(可选)
#cas.authn.jdbc.query[0].fieldExpired=expired
#为不可用字段段,1为不可用,需要修改密码(可选)
#cas.authn.jdbc.query[0].fieldDisabled=disabled
#数据库方言hibernate的
#cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
#数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
#数据库连接
cas.authn.jdbc.query[0].url=jdbc:mysql://192.168.4.15:3306/test01?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
#数据库用户名
cas.authn.jdbc.query[0].user=root
#数据库密码
cas.authn.jdbc.query[0].password=123456
#默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
cas.authn.jdbc.query[0].passwordEncoder.type=com.ucas.CustomPasswordEncoder
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
#Query Database Authentication 数据库查询校验用户名结束

其中,cas.authn.jdbc.query[0].fieldPassword是查询数据库后的密码,cas.authn.jdbc.query[0].sql查询时必须包括这个密码的字段。cas.authn.jdbc.query[0].passwordEncoder.type是用来指定要采用哪种加密方式,NONE表示不加密,Default表示默认,而我们要的自定义加密方式就在此处啦,根据你之前配好的java源目录下的路径,创建一个自定义的加密类,把他的路径拿过来,配置到此处,表示自定义加密,我的路径对应如图:

    cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm表示使用哪个加密算法,我这里使用MD5加密。 

   最后我们只需要实现CustomPasswordEncoder这个自定义类的方法就可以实现自定义加密了。

   首先,这个类必须实现PasswordEncoder的接口,重写里面的两个方法,encode()表示你对密码进行加密,加密后的结果;matches()表示你输入的密码经过加密后和数据库的密码进行比对(数据库密码是已经加密完成的密文)。

   我的加密算法是MD5加盐加密循环3次,所以,这里推荐使用shiro封装的一个类SimpleHash(如果你要使用这种方式,请到pom导入shiro的jar包,这个就不贴了),这个类直接就可以进行MD5多次加密,非常方便。此次贴上我的自定义加密类的代码。

public class CustomPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence password) {

        try {
            //给数据进行md5加密,第一个参数是算法,第二个参数是密码,第三个参数是盐,第四个参数是要
            //循环加密几次
            SimpleHash hash = new SimpleHash("MD5", "admin", "awdwa", 3);
            return hash.toString();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean matches(CharSequence charSequence, String encodePassword) {
        // 判断密码是否存在
        if (charSequence == null) {
            return false;
        }

        //通过md5加密后的密码
        String pass = this.encode(charSequence.toString());
        //比较密码是否相等的问题
        return pass.equals(encodePassword);

    }
}

走到这一步,我们的cas服务端就搭建完毕啦,最后贴上我的application.properties的内容:

##
# CAS Server Context Configuration
#
server.context-path=/cas
server.port=8443

#你的tomcat.keystore的路径
server.ssl.key-store=file:F:/tomcat.keystore
#你之前在配置https时配置的密码
server.ssl.key-store-password=asdasd
#同上
server.ssl.key-password=asdasd

server.max-http-header-size=2097152
server.use-forward-headers=true
server.connection-timeout=20000
server.error.include-stacktrace=ALWAYS

server.compression.enabled=true
server.compression.mime-types=application/javascript,application/json,application/xml,text/html,text/xml,text/plain

server.tomcat.max-http-post-size=2097152
server.tomcat.basedir=build/tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)
server.tomcat.accesslog.suffix=.log
server.tomcat.min-spare-threads=10
server.tomcat.max-threads=200
server.tomcat.port-header=X-Forwarded-Port
server.tomcat.protocol-header=X-Forwarded-Proto
server.tomcat.protocol-header-https-value=https
server.tomcat.remote-ip-header=X-FORWARDED-FOR
server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

##
# CAS Cloud Bus Configuration
#
spring.cloud.bus.enabled=false

# Indicates that systemPropertiesOverride can be used.
# Set to false to prevent users from changing the default accidentally. Default true.
spring.cloud.config.allow-override=true

# External properties should override system properties.
spring.cloud.config.override-system-properties=false

# When allowOverride is true, external properties should take lowest priority, and not override any
# existing property sources (including local config files).
spring.cloud.config.override-none=false

# spring.cloud.bus.refresh.enabled=true
# spring.cloud.bus.env.enabled=true
# spring.cloud.bus.destination=CasCloudBus
# spring.cloud.bus.ack.enabled=true

endpoints.enabled=false
endpoints.sensitive=true

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

# Control the security of the management/actuator endpoints
# The 'enabled' flag below here controls the rendering of details for the health endpoint amongst other things.
management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

# Define a CAS-specific "WARN" status code and its order
management.health.status.order=WARN, DOWN, OUT_OF_SERVICE, UNKNOWN, UP

# Control the security of the management/actuator endpoints
# With basic authentication, assuming Spring Security and/or relevant modules are on the classpath.
security.basic.authorize-mode=role
security.basic.path=/cas/status/**
# security.basic.enabled=true
# security.user.name=casuser
# security.user.password=

##
# CAS Web Application Session Configuration
#
server.session.timeout=300
server.session.cookie.http-only=true
server.session.tracking-modes=COOKIE

##
# CAS Thymeleaf View Configuration
#
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=true
spring.thymeleaf.mode=HTML
spring.thymeleaf.template-resolver-order=100
##
# CAS Log4j Configuration
#
# logging.config=file:/etc/cas/log4j2.xml
server.context-parameters.isLog4jAutoInitializationDisabled=true

##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true

##
# CAS Authentication Credentials
#
#cas.authn.accept.users=casuser::Mellon



#Query Database Authentication 数据库查询校验用户名开始
#查询账号密码sql,必须包含密码字段
cas.authn.jdbc.query[0].sql=select password from sys_user where username=?
#指定上面的sql查询字段名(必须)
cas.authn.jdbc.query[0].fieldPassword=password
#指定过期字段,1为过期,若过期不可用(可选)
#cas.authn.jdbc.query[0].fieldExpired=expired
#为不可用字段段,1为不可用,需要修改密码(可选)
#cas.authn.jdbc.query[0].fieldDisabled=disabled
#数据库方言hibernate的
#cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
#数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
#数据库连接
cas.authn.jdbc.query[0].url=jdbc:mysql://192.168.4.15:3306/test01?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
#数据库用户名
cas.authn.jdbc.query[0].user=root
#数据库密码
cas.authn.jdbc.query[0].password=123456
#默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
cas.authn.jdbc.query[0].passwordEncoder.type=com.ucas.CustomPasswordEncoder
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
#Query Database Authentication 数据库查询校验用户名结束

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要在Apereo CAS 5.2.3服务器端进行增量开发并自定义登录页面、添加验证码、注册和修改功能,可以按照以下步骤进行操作: 1. 下载并安装Apereo CAS 5.2.3服务器端,确保服务器环境和依赖项已正确配置。 2. 创建自定义登录页:在CAS服务器的webapp目录中创建一个新的文件夹,例如customlogin,然后在该文件夹中创建一个HTML文件,可以命名为login.html。在该文件中编写自定义的登录页面,包括用户名和密码输入框和提交按钮等。 3. 更新CAS配置文件:在CAS服务器的/etc/cas目录下找到配置文件cas.properties,找到并修改以下两个属性: cas.login.viewResolver.basename=/customlogin cas.login.viewResolver.suffix=.html 将上述两行配置添加到配置文件中,并设置自定义登录页路径的前缀和后缀。 4. 添加验证码功能:可以使用各种验证码库,如Kaptcha或Google的reCAPTCHA来实现验证码功能。将所选库的相关文件和配置添加到CAS服务器的相应目录中。然后,将验证码相关的代码添加到自定义登录页面中,例如在登录表单中添加一个验证码输入框。 5. 实现注册和修改功能:首先,在CAS服务器的webapp目录中创建一个新的文件夹,例如user,用于处理注册和修改相关的请求。然后,创建相应的HTML文件用于显示注册和修改表单,以及处理相关请求的后端代码。 6. 更新CAS配置文件:在cas.properties中添加以下配置: cas.logout.success.url=/user/logout.html 这将重定向用户到自定义的登出页面。 7. 编写后端代码:在新创建的user目录中编写处理注册和修改相关请求的后端代码,包括验证表单数据和将数据保存到数据库等操作。 8. 部署和测试:将CAS服务器重新启动,并使用浏览器访问自定义的登录页面,尝试登录、注册和修改等操作,确保功能正常运行。 请注意,上述步骤仅提供了一个概述,并且可能需要根据具体需求进行适当的修改和调整。此外,CAS的版本升级可能会对上述步骤产生影响,因此建议在实施前先了解相关的CAS文档和社区讨论。 ### 回答2: 首先,在Apereo CAS 5.2.3服务器端进行增量开发时,需要了解CAS的整体架构和登录流程。CAS使用Spring Security作为身份验证和授权框架,因此我们可以通过自定义Spring Security的配置来实现自定义登录页、增加验证码、注册和修改功能。 1. 自定义登录页: 创建一个自定义的登录页,可以通过使用CAS的主题功能来实现CAS的主题功能允许我们定义自己的视图和控制器。可以在CAS配置文件中进行如下配置: ```properties cas.viewResolver.viewNames[0]=custom_login_view cas.viewResolver.​custom_login_view​=classpath:/templates/custom_login_view.html ``` 在该配置中,我们将自定义的登录页命名为`custom_login_view`,并将其路径设置为`classpath:/templates/custom_login_view.html`。 2. 增加验证码: 为了增加验证码功能,我们可以使用Spring Security的表单登录配置。可以在CAS的配置文件中添加如下配置: ```properties cas.authn.​attributeRepository​[0]=org.apereo.cas.authentication.attribute.DefaultAttributeDefinitionStore cas.authn.​attributeRepository​[0].attributes​=attributeName:value ``` 在该配置中,我们可以将自定义的属性添加到登录表单中,例如`attributeName`和`value`。 3. 注册功能: 如果想要在CAS中添加注册功能,可以使用自定义的服务注册表来实现。可以创建自定义的注册控制器和视图,并在CAS的配置文件中进行如下配置: ```properties cas.viewResolver.viewNames[0]=custom_register_view cas.viewResolver.​custom_register_view​=classpath:/templates/custom_register_view.html ``` 在自定义的注册视图中,可以添加表单输入字段以收集用户注册信息。然后可以在自定义的注册控制器中处理该表单的提交,将用户信息存储到数据库或其他持久化层。 4. 修改功能: 要实现修改功能,可以使用CAS提供的扩展点和API来自定义修改逻辑。可以创建自定义的修改控制器和视图,并在CAS的配置文件中进行如下配置: ```properties cas.viewResolver.viewNames[0]=custom_modify_view cas.viewResolver.​custom_modify_view​=classpath:/templates/custom_modify_view.html ``` 在自定义的修改视图中,可以添加表单输入字段以收集用户修改信息。然后可以在自定义的修改控制器中处理该表单的提交,更新用户信息。 通过以上步骤,可以在Apereo CAS 5.2.3服务器端实现自定义登录页、增加验证码、注册和修改功能。需要注意的是,以上示例仅提供了一种实现方式,具体的实现方式可能因项目需求和环境而有所不同。 ### 回答3: 首先,为了进行apereo CAS 5.2.3 server的增量开发,我们需要确保已经成功安装了CAS server并进行了基本配置。接下来,我们将手把手教你如何自定义登录页,增加验证码以及实现注册和修改功能。 1. 自定义登录页: - 在CAS server的配置文件中,找到`cas.properties`文件,并将其复制到`/etc/cas/config/`目录中。 - 打开复制后的`cas.properties`文件,找到`cas.viewResolver`属性并设置为`default`。 - 在`/etc/cas/config/views/`目录下创建自定义登录页的模板文件,例如`login.jsp`。 - 在`login.jsp`中进行自定义设计,并保存文件。 - 在CAS server的`WebContent`目录下找到`WEB-INF/web.xml`文件,并将其中的`welcome-file`设置为`login.jsp`。 - 重新启动CAS server自定义登录页将会生效。 2. 增加验证码: - 在CAS server的配置文件中,找到`application.properties`文件,并将其复制到`/etc/cas/config/`目录中。 - 打开复制后的`application.properties`文件,找到验证码相关的属性,并进行相应的配置,例如启用验证码、验证码长度,以及验证码的字体、颜色等。 - 在CAS server的`WebContent`目录下找到`WEB-INF/web.xml`文件,添加验证码相关的过滤器配置。 - 在自定义登录页的表单中,添加一个验证码输入框,并在表单提交时进行验证码的验证。 - 重新启动CAS server,验证码将会在登录时显示并进行验证。 3. 实现注册和修改功能: - 在CAS server中创建一个自定义的注册和修改模块,可以基于现有的模块进行扩展或自行开发。 - 在CAS server的配置文件中,找到`cas.properties`文件,并将其复制到`/etc/cas/config/`目录中。 - 打开复制后的`cas.properties`文件,找到`cas.authn.accept.users`属性并添加一个用于注册和修改的用户类型。 - 在注册和修改模块中,实现相应的业务逻辑,例如验证用户输入、更新用户信息等。 - 在自定义登录页中添加注册和修改的链接,并配置相应的路径。 - 重新启动CAS server,注册和修改功能将会生效。 以上就是手把手教你如何在apereo CAS 5.2.3 server上进行增量开发,包括自定义登录页、增加验证码以及实现注册和修改功能的步骤。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值