1. 程式人生 > >spring boot 引用 shiro 認證AD域

spring boot 引用 shiro 認證AD域

1、首先搭建AD域

     地址192.168.0.112

     域名gcloud.com

2、編輯配置檔案shiro.ini

    

activeDirectoryRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm
activeDirectoryRealm.url = ldap://192.168.0.112:389 
activeDirectoryRealm.principalSuffix = @gcloud.com

 

3、測試程式碼

   

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;


public class AdRealm {
    public static final String userName = "user";
    public static final String password = "passwd";

    public static void main(String[] args) {
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);


        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        Subject currentUser = SecurityUtils.getSubject();
        try {
            currentUser.login(token);
            System.out.println("認證成功!");
        } catch (AuthenticationException e) {
            System.out.println("認證失敗!確認使用者名稱密碼是否正確!");
            e.printStackTrace();
        }
    }
}

4、認證結果