大家好,又见面了,我是你们的朋友全栈君。
1.关于AD域的介绍
AD的全称是Active Directory:活动目录
域(Domain):
1)域是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系(即Trust Relation)。信任关系是连接在域与域之间的桥梁。当一个域与其他域建立了信任关系后
2)两个域之间不但可以按需要相互进行管理,还可以跨网分配文件和打印机等设备资源,使不同的域之间实现网络资源的共享与管理,以及相互通信和数据传输
域控制器(DC):
域控制器就是一台服务器,负责每一台联入网络的电脑和用户的验证工作。
组织单元(OU)
用户名服务器名(CN)
2.相关工具
LDAP管理工具 下载
相关的安装连接步骤就不详细介绍,连接成功截图,即可获取整个集团服务器域的信息
3.与shiro集成开发登录验证
3.1 全局配置
ldap.url = ldap://10.0.58.222:389/
ldap.org = ou\=\u6F4D\u6fF4\u7328\u6237,dc\=weichai,dc\=com
ldap.user = ####
ldap.password = #####
ldap.ldapfactory = com.sun.jndi.ldap.LdapCtxFactory
ldap.authentication =simple
3.2 获取AD全局配置信息
/**
* Created by llhy n on 2019/7/21.
* AD域参数配置
*/
public class AdConfig {
private String url;
private String ldapfactory;
private String authentication;
private String org;
private String user;
private String password;
private static AdConfig adConfig = null;
public AdConfig() {
url = Global.getConfig("ldap.url");
ldapfactory = Global.getConfig("ldap.ldapfactory");
authentication = Global.getConfig("ldap.authentication");
org = Global.getConfig("ldap.org");
user = Global.getConfig("ldap.user");
password = Global.getConfig("ldap.password");
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getLdapfactory() {
return ldapfactory;
}
public void setLdapfactory(String ldapfactory) {
this.ldapfactory = ldapfactory;
}
public String getAuthentication() {
return authentication;
}
public void setAuthentication(String authentication) {
this.authentication = authentication;
}
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static AdConfig getAdConfig() {
if (adConfig == null){
adConfig = new AdConfig();
}
return adConfig;
}
}
3.3 shiro文件的配置
<!--AD 更改-->
<!-- 3.1 直接配置继承了org.apache.shiro.realm.AuthorizingRealm的bean -->
<bean id="userRealm" class="com.weichai.modules.sys.security.SystemAuthorizingRealm">
<!-- 配置密码匹配器 -->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!-- 凭证匹配器 -->
<bean id="credentialsMatcher" class="com.weichai.modules.sys.security.CustomCredentialsMatcher">
</bean>
3.4 登录身份认证后端
/**
* 系统安全认证实现类
* @author lhy
* @version 2019-7-5
*/
public class SystemAuthorizingRealm extends AuthorizingRealm {
private Logger logger = LoggerFactory.getLogger(getClass());
private SystemService systemService;
public SystemAuthorizingRealm() {
this.setCachingEnabled(false);
}
/**
* 认证回调函数, 登录时调用
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
/*int activeSessionSize = getSystemService().getSessionDao().getActiveSessions(false).size();
if (logger.isDebugEnabled()){
logger.debug("login submit, active session size: {}, username: {}", activeSessionSize, token.getUsername());
}*/
//获取图片二维码
Session session = UserUtils.getSession();
String code = (String)session.getAttribute(ValidateCodeServlet.VALIDATE_CODE);
// 校验登录验证码
if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)){
if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)){
throw new AuthenticationException("msg:验证码错误, 请重试.");
}
}
if(StringUtils.isBlank(String.valueOf(token.getPassword()))){
throw new AuthenticationException("msg:密码不能为空");
}
// 校验用户名密码
User user = getSystemService().getUserByLoginNameLogin(token.getUsername());
if (user != null) {
//特权账户
if ("1".equals(user.getIsPriv())) {
if (StringUtils.isBlank(token.getCaptcha()) || StringUtils.isBlank(token.getSmsCode())) {
throw new AuthenticationException("msg:请使用短信验证码登陆.");
}
//特权账户校验图片验证码
if (!token.getCaptcha().toUpperCase().equals(code)) {
throw new AuthenticationException("msg:图片码验证码验证失败, 请重试.");
}
try {
validateSMS(token.getUsername(), token.getSmsCode(), token.getHost());
} catch (SmsCodeValidateException e) {
throw new AuthenticationException("msg:" + e.getMessage());
}
}
if (Global.NO.equals(user.getLoginFlag())) {
throw new AuthenticationException("msg:该已帐号禁止登录.");
}
if (user.getIsAd() != null && 1 == user.getIsAd()) {
openAD(token.getUsername(), String.valueOf(token.getPassword()));
token.setPass(true);
}
byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16));
return new SimpleAuthenticationInfo(new Principal(user, token.isMobileLogin()),
user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName());
} else {
return null;
}
}
/**
* 校验短信验证码
* @param userName 用户名
* @param smsCode 短信验证码
* @param host 用户ip
* @return
*/
protected boolean validateSMS(String userName, String smsCode, String host) throws SmsCodeValidateException {
String proName = Global.getConfig("cas.appid");
String sendUrl = Global.getConfig("priv.validate");
Map<String, Object> map = new HashMap<>();
map.put("userName", userName);
map.put("proName", proName);
map.put("ip", host);
map.put("valCode", smsCode);
String result = null;
try {
result = HttpUtil.post(sendUrl, map);
logger.error("[短信验证码校验结果]" + result);
} catch (Exception e) {
logger.error("[特权账户短信验证接口调用失败]" + e);
return false;
}
JSONObject obj = JSONObject.parseObject(result);
int code = (int) obj.get("code");
String msg = (String)obj.get("msg");
if (code == 0) {
logger.error("[短信验证码校验通过]");
return true;
}
logger.error("[短信验证码校验未通过,原因:"+msg+"]");
throw new SmsCodeValidateException(msg);
}
private void openAD(String username, String password) {
DirContext ctx = null;
AdConfig adf = AdConfig.getAdConfig();
Hashtable env = new Hashtable();
if(StringUtils.isBlank(password)||StringUtils.isBlank(username)){
throw new AuthenticationException("msg:用户名或者密码不正确!");
}
try {
env.put(Context.PROVIDER_URL, adf.getUrl() + URLEncoder.encode(adf.getOrg(), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String org=adf.getOrg();
String dc=org.replaceFirst(",dc=","@");
dc=dc.substring(dc.indexOf("@"));
dc=dc.replaceFirst(",dc=",".");
env.put(Context.SECURITY_PRINCIPAL, username+dc);
env.put(Context.SECURITY_CREDENTIALS,password);
env.put(Context.INITIAL_CONTEXT_FACTORY,adf.getLdapfactory());
env.put(Context.SECURITY_AUTHENTICATION, adf.getAuthentication());
try {
ctx = new InitialDirContext(env);// 初始化上下文
} catch (javax.naming.AuthenticationException e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域身份验证失败!");
} catch (javax.naming.CommunicationException e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域连接失败!");
} catch (Exception e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域身份验证未知异常!");
} finally{
if(null!=ctx){
try {
ctx.close();
ctx=null;
} catch (Exception e) {
logger.error(e.toString());
}
}
}
}
/**
* 获取权限授权信息,如果缓存中存在,则直接从缓存中获取,否则就重新获取, 登录成功后调用
*/
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
return null;
}
AuthorizationInfo info = null;
info = (AuthorizationInfo)UserUtils.getCache(UserUtils.CACHE_AUTH_INFO);
if (info == null) {
info = doGetAuthorizationInfo(principals);
if (info != null) {
UserUtils.putCache(UserUtils.CACHE_AUTH_INFO, info);
}
}
return info;
}
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Principal principal = (Principal) getAvailablePrincipal(principals);
// 获取当前已登录的用户
if (!Global.TRUE.equals(Global.getConfig("user.multiAccountLogin"))){
Collection<Session> sessions = getSystemService().getSessionDao().getActiveSessions(true, principal, UserUtils.getSession());
if (sessions.size() > 0){
// 如果是登录进来的,则踢出已在线用户
if (UserUtils.getSubject().isAuthenticated()){
for (Session session : sessions){
getSystemService().getSessionDao().delete(session);
}
}
// 记住我进来的,并且当前用户已登录,则退出当前用户提示信息。
else{
UserUtils.getSubject().logout();
throw new AuthenticationException("msg:账号已在其它地方登录,请重新登录。");
}
}
}
User user = getSystemService().getUserByLoginName(principal.getLoginName());
if (user != null) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
List<Menu> list = UserUtils.getMenuList();
for (Menu menu : list){
if (StringUtils.isNotBlank(menu.getPermission())){
// 添加基于Permission的权限信息
for (String permission : StringUtils.split(menu.getPermission(),",")){
info.addStringPermission(permission);
}
}
}
// 添加用户权限
info.addStringPermission("user");
// 添加用户角色信息
for (Role role : user.getRoleList()){
info.addRole(role.getEnname());
}
// 更新登录IP和时间
getSystemService().updateUserLoginInfo(user);
// 记录登录日志
LogUtils.saveLog(Servlets.getRequest(), "系统登录");
return info;
} else {
return null;
}
}
@Override
protected void checkPermission(Permission permission, AuthorizationInfo info) {
authorizationValidate(permission);
super.checkPermission(permission, info);
}
@Override
protected boolean[] isPermitted(List<Permission> permissions, AuthorizationInfo info) {
if (permissions != null && !permissions.isEmpty()) {
for (Permission permission : permissions) {
authorizationValidate(permission);
}
}
return super.isPermitted(permissions, info);
}
@Override
public boolean isPermitted(PrincipalCollection principals, Permission permission) {
authorizationValidate(permission);
return super.isPermitted(principals, permission);
}
@Override
protected boolean isPermittedAll(Collection<Permission> permissions, AuthorizationInfo info) {
if (permissions != null && !permissions.isEmpty()) {
for (Permission permission : permissions) {
authorizationValidate(permission);
}
}
return super.isPermittedAll(permissions, info);
}
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
/**
* 授权验证方法
* @param permission
*/
private void authorizationValidate(Permission permission){
// 模块授权预留接口
}
/**
* 设定密码校验的Hash算法与迭代次数
*/
@PostConstruct
public void initCredentialsMatcher() {
CustomCredentialsMatcher matcher = new CustomCredentialsMatcher(SystemService.HASH_ALGORITHM);
matcher.setHashIterations(SystemService.HASH_INTERATIONS);
setCredentialsMatcher(matcher);
}
// /**
// * 清空用户关联权限认证,待下次使用时重新加载
// */
// public void clearCachedAuthorizationInfo(Principal principal) {
// SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
// clearCachedAuthorizationInfo(principals);
// }
/**
* 清空所有关联认证
* @Deprecated 不需要清空,授权缓存保存到session中
*/
@Deprecated
public void clearAllCachedAuthorizationInfo() {
// Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
// if (cache != null) {
// for (Object key : cache.keys()) {
// cache.remove(key);
// }
// }
}
/**
* 获取系统业务对象
*/
public SystemService getSystemService() {
if (systemService == null){
systemService = SpringContextHolder.getBean(SystemService.class);
}
return systemService;
}
/**
* 授权用户信息
*/
public static class Principal implements Serializable {
private static final long serialVersionUID = 1L;
private String id; // 编号
private String loginName; // 登录名
private String name; // 姓名
private boolean mobileLogin; // 是否手机登录
// private Map<String, Object> cacheMap;
public Principal(User user, boolean mobileLogin) {
this.id = user.getId();
this.loginName = user.getLoginName();
this.name = user.getName();
this.mobileLogin = mobileLogin;
}
/***
*
* @param user
* @param mobileLogin
* @param pass 是否跳过验证
*/
public Principal(User user, boolean mobileLogin,boolean pass) {
this.id = user.getId();
this.loginName = user.getLoginName();
this.name = user.getName();
this.mobileLogin = mobileLogin;
}
public String getId() {
return id;
}
public String getLoginName() {
return loginName;
}
public String getName() {
return name;
}
public boolean isMobileLogin() {
return mobileLogin;
}
// @JsonIgnore
// public Map<String, Object> getCacheMap() {
// if (cacheMap==null){
// cacheMap = new HashMap<String, Object>();
// }
// return cacheMap;
// }
/**
* 获取SESSIONID
*/
public String getSessionid() {
try{
return (String) UserUtils.getSession().getId();
}catch (Exception e) {
return "";
}
}
@Override
public String toString() {
return id;
}
}
}
3.5 AD域认证测试类
/**
* AD域本地连接测试
* @author linhaiy
* @version 2019.07.20
*/
public class AdTest {
private static Logger logger = LoggerFactory.getLogger(AdTest.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
String username = "admin";
String password = "Admin12356!!";
openAD(username,password);
}
@SuppressWarnings("unchecked")
private static void openAD(String username, String password) {
DirContext ctx = null;
AdConfig adf = AdConfig.getAdConfig();
Hashtable env = new Hashtable();
if (StringUtils.isBlank(password) || StringUtils.isBlank(username)) {
throw new AuthenticationException("msg:用户名或者密码不正确!");
}
try {
env.put(Context.PROVIDER_URL, adf.getUrl() + URLEncoder.encode(adf.getOrg(), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String org = adf.getOrg();
String dc = org.replaceFirst(",dc=", "@");
dc = dc.substring(dc.indexOf("@"));
dc = dc.replaceFirst(",dc=", ".");
env.put(Context.SECURITY_PRINCIPAL, username + dc);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, adf.getLdapfactory());
env.put(Context.SECURITY_AUTHENTICATION, adf.getAuthentication());
try {
ctx = new InitialDirContext(env);// 初始化上下文
} catch (javax.naming.AuthenticationException e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域身份验证失败!");
} catch (javax.naming.CommunicationException e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域连接失败!");
} catch (Exception e) {
logger.error(e.toString());
throw new AuthenticationException("msg:AD域身份验证未知异常!");
} finally {
if (null != ctx) {
try {
ctx.close();
ctx = null;
} catch (Exception e) {
logger.error(e.toString());
}
}
}
}
}
这样就增加了域用户验证登录功能,即用户在登录时既可以使用数据库里的用户,也可以使用域服务器上的域用户和密码登录
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/145452.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...