需求:根據配置文件配置,設置是否開啓AD域驗證。java
第一步:修改jeesite.properties配置文件web
ldap.url = ldap://ip地址:端口/ ldap.org = ou=XXXXXXXX,dc=XXXXX,dc=com ldap.user = username ldap.password = password ldap.ldapfactory = com.sun.jndi.ldap.LdapCtxFactory ldap.authentication = simple #AD域登陸驗證是否開啓,true:開啓;false:關閉 ldap.adopen = true
第二步:自定義shiro密碼驗證方法。算法
1. 修改spring-context-shiro.xmlspring
<!-- 定義Shiro安全管理配置 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--<property name="realm" ref="systemAuthorizingRealm" />-->//註釋掉 <property name="realm" ref="userRealm" />//本身定義 <property name="sessionManager" ref="sessionManager" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 3.1 直接配置繼承了org.apache.shiro.realm.AuthorizingRealm的bean --> <bean id="userRealm" class="com.test.modules.sys.security.SystemAuthorizingRealm"> <!-- 配置密碼匹配器 --> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <!-- 憑證匹配器 --> <bean id="credentialsMatcher" class="com.test.modules.sys.security.CustomCredentialsMatcher"> </bean>
2. 新增自定義密碼匹配類apache
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher { @Override public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) { return true; } }
3. 修改SystemAuthorizingRealm.java文件initCredentialsMatcher方法安全
這個類在項目啓動時自動注入,這個方法在注入時自動調用,根據配置文件選擇加載匹配器。session
/** * 設定密碼校驗的Hash算法與迭代次數 */ @PostConstruct public void initCredentialsMatcher() { //根據配置文件,在項目加載時加載密碼匹配方式 if("true".equals(openADFlag)){//openADFlag爲配置文件中AD域是否開啓標誌 setCredentialsMatcher(new CustomCredentialsMatcher()); }else{ HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SystemService.HASH_ALGORITHM); matcher.setHashIterations(SystemService.HASH_INTERATIONS); setCredentialsMatcher(matcher); } }
第三步:修改SystemAuthorizingRealm.java中doGetAuthenticationInfoide
該方法:認證回調函數, 登陸時調用函數
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; // 校驗登陸驗證碼 if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)){ Session session = UserUtils.getSession(); String code = (String)session.getAttribute(ValidateCodeServlet.VALIDATE_CODE); if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)){ throw new AuthenticationException("msg:驗證碼錯誤, 請重試."); } } /* 1.讀取配置文件,是否進行AD域驗證 2.若是否,則繼續往下走,使用原來的流程 3.若是是,獲取到用戶後就不須要進行密碼驗證了。 */ if("true".equals(openADFlag)){ openAD(token.getUsername(),String.valueOf(token.getPassword())); } // 校驗用戶名密碼 User user = getSystemService().getUserByLoginName(token.getUsername()); if (user != null) { if (Global.NO.equals(user.getLoginFlag())){ throw new AuthenticationException("msg:該已賬號禁止登陸."); } byte[] salt = Encodes.decodeHex(user.getPassword().substring(0,16)); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(new Principal(user, token.isMobileLogin()), user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName()); return info; } else { if("true".equals(openADFlag)){ throw new AuthenticationException("msg:請聯繫系統管理員."); } return null; } }
第四步:添加AD域驗證方法url
//判斷是否須要AD域驗證 protected Boolean openAD(String username, String password){ DirContext ctx = null; AdConfig adf = AdConfig.getAdConfig(); Hashtable env = new Hashtable(); 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);// 初始化上下文 System.out.println("身份驗證成功!"); return true; } catch (javax.naming.AuthenticationException e) { e.printStackTrace(); throw new AuthenticationException("msg:AD域身份驗證失敗."); } catch (javax.naming.CommunicationException e) { e.printStackTrace(); throw new AuthenticationException("msg:AD域鏈接失敗!"); } catch (Exception e) { e.printStackTrace(); throw new AuthenticationException("msg:AD域身份驗證未知異常!"); } finally{ if(null!=ctx){ try { ctx.close(); ctx=null; } catch (Exception e) { e.printStackTrace(); } } } }