ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1、基于form表单的普通登录方式 继承org.walkframework.shiro.realm.BaseUserRealm ~~~ public class UserRealm extends BaseUserRealm { private Common common = SingletonFactory.getInstance(Common.class); /** * token支持类型定义 * * @param token * @return */ @Override public boolean support(AuthenticationToken token) { return token instanceof FormToken; } /** * 获取用户认证信息 * * @param token * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetUserAuthenticationInfo(BaseToken token){ FormToken formToken = (FormToken)token; String staffId = formToken.getUsername(); //从数据库中获取用户信息 TdMStaffExt staffInfo = getUserService().findUser(staffId); if(staffInfo == null){ log.error("未知账户[{}]", staffId); throw new UnknownAccountException(); } //从数据库中获取部门信息 TdMDepart departInfo = getUserService().findOrganization(staffInfo.getDepartId()); //将用户信息设置到自定义的principal中 StaffPrincipal principal = new StaffPrincipal(formToken); principal.setStaffId(staffInfo.getStaffId()); principal.setStaffName(staffInfo.getStaffName()); principal.setSerialNumber(staffInfo.getSerialNumber()); principal.setEparchyCode(staffInfo.getEparchyCode()); principal.setCityCode(staffInfo.getCityCode()); principal.setTeamCode(staffInfo.getTeamCode()); principal.setDepart(departInfo); //将身份信息设置到token中 token.setPrincipal(principal); //正常应该是取数据库密码staffInfo.getStaffPasswd()。此示例工程省略密码校验环节 //return new SimpleAuthenticationInfo(principal, staffInfo.getStaffPasswd(), getName()); return new SimpleAuthenticationInfo(principal, token.getCredentials(), getName()); } /** * 获取用户授权信息 * * @param principals * @return * @throws AuthenticationException */ @Override protected AuthorizationInfo doGetUserAuthorizationInfo(PrincipalCollection principals, SimpleAuthorizationInfo authorizationInfo) { StaffPrincipal principal = (StaffPrincipal)principals.getPrimaryPrincipal(); String staffId = principal.getStaffId(); try { //查询用户拥有角色列表 List<String> roles = getUserService().findRoles(staffId); authorizationInfo.addRoles(roles); //查询用户拥有权限列表 List<String> permissions = getUserService().findPermissions(staffId); authorizationInfo.addStringPermissions(permissions); TdMStaff staffInfo = getUserService().findUser(staffId); //系统管理员/地州管理员/省份管理员 if ("0".equals(staffInfo.getJobCode()) || "1".equals(staffInfo.getJobCode())) { authorizationInfo.addRole(RoleContants.EPARCHY_MANAGER); } //团队长 else if ("2".equals(staffInfo.getJobCode()) && "1".equals(staffInfo.getIsTeamManager())){ authorizationInfo.addRole(RoleContants.TEAM_LEADER); } //客户经理 else if ("2".equals(staffInfo.getJobCode()) && !"1".equals(staffInfo.getIsTeamManager())){ authorizationInfo.addRole(RoleContants.CUST_MANAGER); } } catch (Exception e) { common.error("获取权限信息出错!", e); } return authorizationInfo; } } ~~~ 2、基于CAS的单点登录方式 继承org.walkframework.shiro.realm.BaseCasRealm ~~~ public class SsoCasRealm extends BaseCasRealm { private Common common = SingletonFactory.getInstance(Common.class); /** * token支持类型定义 * * @param token * @return */ @Override public boolean support(AuthenticationToken token) { return token instanceof CasToken; } /** * 获取用户认证信息 * * @param token * @param casPrincipal * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetUserAuthenticationInfo(BaseToken token, AttributePrincipal casPrincipal) throws AuthenticationException { //从cas服务端取得用户身份信息 IData<String, Object> staffInfo = new DataMap<String, Object>(); staffInfo.putAll(casPrincipal.getAttributes()); //将用户信息设置到自定义的principal中 StaffPrincipal principal = new StaffPrincipal(token); principal.setStaffId(staffInfo.getString("STAFF_ID")); principal.setStaffName(staffInfo.getString("STAFF_NAME")); principal.setDepartId(staffInfo.getString("DEPART_ID")); principal.setDepartName(staffInfo.getString("DEPART_NAME")); principal.setDepartCode(staffInfo.getString("DEPART_CODE")); principal.setCityId(staffInfo.getString("CITY_CODE")); principal.setCityName(staffInfo.getString("CITY_NAME")); principal.setAreaCode(staffInfo.getString("AREA_CODE")); principal.setAreaName(staffInfo.getString("AREA_NAME")); principal.setEparchyCode(staffInfo.getString("EPARCHY_CODE")); principal.setEparchyName(staffInfo.getString("EPARCHY_NAME")); principal.setCucDepartId(staffInfo.getString("CUC_DEPART_CODE")); principal.setCucEparchyCode(staffInfo.getString("CUC_AREA_CODE")); principal.setProvinceId(staffInfo.getString("PROVINCE_ID")); principal.setProvinceCode(staffInfo.getString("PROVINCE_CODE")); principal.setCbssCheckCode(staffInfo.getString("cbssCheckCode")); //将身份信息设置到token中 token.setPrincipal(principal); return new SimpleAuthenticationInfo(principal, token.getCredentials(), getName()); } /** * 获取用户授权信息 * * @param principals * @param authorizationInfo * @return * @throws AuthenticationException */ @Override protected AuthorizationInfo doGetUserAuthorizationInfo(PrincipalCollection principals, SimpleAuthorizationInfo authorizationInfo) { StaffPrincipal principal = (StaffPrincipal)principals.getPrimaryPrincipal(); String staffId = principal.getStaffId(); try { //查询用户拥有角色列表 List<String> roles = getUserService().findRoles(staffId); authorizationInfo.addRoles(roles); //查询用户拥有权限列表 List<String> permissions = getUserService().findPermissions(staffId); authorizationInfo.addStringPermissions(permissions); } catch (Exception e) { common.error("获取权限信息出错!", e); } return authorizationInfo; } } ~~~ 3、基于静默登录的方式 继承org.walkframework.shiro.realm.BaseSilenceLoginRealm ~~~ public class SilenceLoginRealm extends BaseSilenceLoginRealm { @Resource(name = "loginService") private LoginService loginService; /** * token支持类型定义 * * @param token * @return */ @Override public boolean support(AuthenticationToken token) { return token instanceof SilenceLoginToken; } /** * 获取用户认证信息 * * @param token * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetSilenceUserAuthenticationInfo(BaseToken token){ StaffPrincipal principal = new StaffPrincipal(token); SilenceLoginToken silenceToken = (SilenceLoginToken)token; final String staffId = silenceToken.getUsername(); IData<String, Object> retInfo = loginService.loginWosale(staffId, null, "2"); String result = retInfo.getString("result"); if("true".equals(result)){ principal.setStaffId(staffId); principal.setStaffName(retInfo.getString("staffName")); principal.setEparchyCode(retInfo.getString("eparchyId")); principal.setCityCode(retInfo.getString("cityId")); principal.setDepartId(retInfo.getString("departId")); principal.setDepartName(retInfo.getString("departName")); principal.setTokenId(retInfo.getString("sessionId")); } else { throw new IncorrectCredentialsException(retInfo.getString("info")); } //将身份信息设置到token中 token.setPrincipal(principal); return new SimpleAuthenticationInfo(principal, silenceToken.getSign(), getName()); } /** * 获取用户授权信息 * * @param principals * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { return null; } /** * 获取用户授权信息 * * @param principals * @return */ @Override protected AuthorizationInfo doGetUserAuthorizationInfo(PrincipalCollection principals, SimpleAuthorizationInfo authorizationInfo) { return null; } ~~~