单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密
我们在前一篇文章中实现了cas4.2.x登录使用mongodb验证方式。
单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
也学习参考了cas5.0.x版本的mongodb的四种加密方式。
单点登录(十四)-----实战-----cas5.0.x登录mongodb验证方式常规的四种加密的思考和分析
结合到cas 4.2.x的代码情况我们发现需要修改MongoAuthenticationHandler.java部分的代码才可以实现自定义加密。
主要思路是我们需要新建一个class 实现org.pac4j.http.credentials.password.PasswordEncoder接口。
然后让MongoAuthenticator验证的时候使用这个加密class即可。
详细步骤参考如下(我们这里来实现cas5.0中的DefaultPasswordEncoder的MD5加密):
新建一个名为DefaultPasswordEncoderMD5的class实现org.pac4j.http.credentials.password.PasswordEncoder接口,并且把org.jasig.cas.authentication.handler包中的DefaultPasswordEncoder实现粘贴过来如下:
package org.jasig.cas.authentication;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
import org.pac4j.http.credentials.password.PasswordEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultPasswordEncoderMD5 implements PasswordEncoder{
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final int HEX_RIGHT_SHIFT_COEFFICIENT = 4;
private static final int HEX_HIGH_BITS_BITWISE_FLAG = 0x0f;
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPasswordEncoderMD5.class);
private final String encodingAlgorithm="MD5";
private String characterEncoding="UTF-8";
@Override
public String encode(final String password) {
if (password == null) {
return null;
}
if (StringUtils.isBlank(this.encodingAlgorithm)) {
LOGGER.warn("No encoding algorithm is defined. Password cannot be encoded; Returning null");
return null;
}
try {
final MessageDigest messageDigest = MessageDigest.getInstance(this.encodingAlgorithm);
final String encodingCharToUse = StringUtils.isNotBlank(this.characterEncoding)
? this.characterEncoding : Charset.defaultCharset().name();
LOGGER.warn("Using {} as the character encoding algorithm to update the digest", encodingCharToUse);
messageDigest.update(password.getBytes(encodingCharToUse));
final byte[] digest = messageDigest.digest();
return getFormattedText(digest);
} catch (final NoSuchAlgorithmException e) {
throw new SecurityException(e);
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(final byte[] bytes) {
final StringBuilder buf = new StringBuilder(bytes.length * 2);
for (int j = 0; j < bytes.length; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> HEX_RIGHT_SHIFT_COEFFICIENT) & HEX_HIGH_BITS_BITWISE_FLAG]);
buf.append(HEX_DIGITS[bytes[j] & HEX_HIGH_BITS_BITWISE_FLAG]);
}
return buf.toString();
}
public void setCharacterEncoding(final String characterEncoding) {
this.characterEncoding = characterEncoding;
}
}
加密类有了,我们就可以在MongoAuthenticator验证的时候使用了。
需要修改MongoAuthenticationHandler.java里的两个地方。
private org.pac4j.http.credentials.password.PasswordEncoder mongoPasswordEncoder = new NopPasswordEncoder();
修改为新建我们的新class如下:
private org.pac4j.http.credentials.password.PasswordEncoder mongoPasswordEncoder = new DefaultPasswordEncoderMD5();
然后
final MongoAuthenticator mongoAuthenticator = new MongoAuthenticator(client, this.attributes);
中使用这个验证如下:
final MongoAuthenticator mongoAuthenticator = new MongoAuthenticator(client, this.attributes,mongoPasswordEncoder );
即可。
运行程序,这样我们的登录密码就会先根据encode的方法加密后才跟数据库中的密码作对比。
这样我们就实现了自定义加密了。
单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密的更多相关文章
- 单点登录(十四)-----实战-----cas5.0.x登录mongodb验证方式常规的四种加密的思考和分析
我们在上一篇文章中已经讲解了cas4.2.X登录启用mongodb验证方式 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 但是密码是明文存储的,也就是 ...
- 单点登录(十七)----cas4.2.x登录mongodb验证方式成功后返回更多信息更多属性到客户端
我们在之前已经完成了cas4.2.x登录使用mongodb验证方式登录成功了.也解决了登录名中使用中文乱码的问题. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方 ...
- 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...
- 单点登录(十二)-----遇到问题-----cas启用mongodb验证方式登录后没反应-pac4j-mongo包中的MongoAuthenticatInvocationTargetException
cas启用mongodb验证方式登录后没反应 控制台输出 2017-02-09 20:27:15,766 INFO [org.jasig.cas.authentication.MongoAuthent ...
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
- 单点登录(十)-----遇到问题-----cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed
cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed. 完整报错信息: 二月 08, 2017 5 ...
- YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法
上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...
- 十五天精通WCF——第九天 高级玩法之自定义Behavior
终于我又看完了二期爱情保卫战,太酸爽了,推荐链接:http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd,不多说,谁看谁入迷,下面言归正传, 看看这 ...
- 单点登录(十一)-----遇到问题-----cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema na
cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.sp ...
随机推荐
- 【转】phpcms v9的ckeditor加入给内容调整行高
今天公司一客户要求一同事给ckeditor加入可以设置行高的功能(他后台是用织梦做的,他是织梦的FANS),我一时闲得慌,也想给咱家的v9加入这个功能,功夫不负有心啊,终于成功了,来给大家分享一下! ...
- visudo命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/ImJerryChan/p/6667819.html 目录前言一.介绍二.配置文件简介三.实战配置 前言: su ...
- teamwork 2
1.访问上学期项目团队,学习他们的得失. 上学期学长们有一个项目是学霸系统,在看过了学长们的相关博客后,我们可以感受到学长们确实花费了不少心思,也看到了许多值得我们学习的地方. 首先,学长们在项目开始 ...
- jsp九大内置对象之config 和 out
jsp中config的作用是读取web.xml中的配置信息,一般在后台获取初始化的参数,jsp页面用的较少因为jsp属于表现层,一般是获取数据. jsp中的out对象是将内容放到缓冲区中然后显示出来
- c++第七次作业____最后的总结
先言: 在这过程中学到: 第二次作业Github的使用 第四次作业计算器的计算 ps:表达式处理以及计算 第五次作业文件的处理问题 第六次作业界面的设计 总结: 1.这学期的计算器,做的有点匆忙,偶尔 ...
- 四则运算结对项目之GUI
本次结对编程让我学到了许多许多知识,受益匪浅!在此之前,我没想过我能做出一个双击运行的小程序. 感谢我的队友与我同心协力,感谢室友宇欣告诉我操作符为“最多多少”而不是“多少”并教我使用效能分析工具,感 ...
- week3b:个人博客作业
vs2013的初步使用 由于自己在写这篇博客的时候已经把vs2013安装完毕,就我写了. 这是2013的使用步骤 第一步 注释 箭头指的方向是“点击” 第二步 第三步 第四步 第5步 第六步 最后点击 ...
- IDEA2018 license
2018-06-01更新 更新了webstorm 3.2之后发现居然又不能用了,现用 http://idea.congm.in 可以激活 新增一个 http://idea.toocruel.net
- 小程序 上啦下拉刷新window配置
"enablePullDownRefresh": "true" /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefres ...
- 错误“AxImp.exe”已退出,代码为 -1163019603【转载及个人看法】
http://blog.csdn.net/duguduchong/article/details/17166123 最近使用vs2010 在重新生成解决方案的时候出现 “AxImp.exe”已退出, ...