ER图测试,域账号登录测试,修改数据查询的展示问题

This commit is contained in:
暮光:城中城
2021-08-02 21:18:58 +08:00
parent 83ca189598
commit 51f74f60c3
34 changed files with 1795 additions and 89 deletions

View File

@@ -0,0 +1,30 @@
package com.zyplayer.doc.manage.framework.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
@Configurable
public class LdapConfig {
String url = "ldap://10.0.1.1:10389";
String base = "dc=xx,dc=net";
String userDn = "cn=Manager,dc=xx,dc=net";
String password = "MKDSHYDNIS";
/**
* 初始化
*/
@Bean
public LdapTemplate getLdapTemplate() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(url);
contextSource.setBase(base);
contextSource.setUserDn(userDn);
contextSource.setPassword(password);
contextSource.setPooled(false);
contextSource.afterPropertiesSet();
return new LdapTemplate(contextSource);
}
}

View File

@@ -6,41 +6,75 @@ import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.data.config.security.DocUserDetails;
import com.zyplayer.doc.data.config.security.DocUserUtil;
import com.zyplayer.doc.data.repository.manage.entity.UserInfo;
import com.zyplayer.doc.data.service.manage.AuthInfoService;
import com.zyplayer.doc.data.service.manage.UserAuthService;
import com.zyplayer.doc.data.service.manage.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import com.zyplayer.doc.manage.web.manage.param.LdapPerson;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.DirContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* 用户登录控制器
*/
@RestController
public class LoginController {
@Autowired
private UserInfoService userInfoService;
@Autowired
private UserAuthService userAuthService;
@Autowired
private AuthInfoService authInfoService;
@Resource
private UserInfoService userInfoService;
@Resource
private UserAuthService userAuthService;
@Resource
private LdapTemplate ldapTemplate;
// TODO 域账号登录,待测试
@Value("${spring.ldap.domainName:}")
private String ldapDomainName;
@Value("${spring.ldap.urls:}")
private String ldapUrls;
/**
* 用户登录
*/
@PostMapping(value = "/login")
public DocResponseJson<Object> login(String username, String password, HttpServletResponse response) {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_no", username);
queryWrapper.eq("del_flag", 0);
UserInfo userInfo = userInfoService.getOne(queryWrapper);
if (userInfo == null) {
return DocResponseJson.warn("用户名'" + username + "'没有找到!");
}
String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
if (!Objects.equals(userInfo.getPassword(), pwdMd5)) {
return DocResponseJson.warn("密码错误");
// 如果使用域账号登录
if (this.isUseLdapServer()) {
LdapPerson ldapPerson = this.getUserFromLdap(username, password);
if (null == ldapPerson) {
return DocResponseJson.warn("用户名或密码错误");
}
if (userInfo == null) {
userInfo = this.ldapAutoRegister(ldapPerson);
}
} else {
if (userInfo == null) {
return DocResponseJson.warn("用户名'" + username + "'没有找到!");
}
String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
if (!Objects.equals(userInfo.getPassword(), pwdMd5)) {
return DocResponseJson.warn("用户名或密码错误");
}
}
Set<String> userAuthSet = userAuthService.getUserAuthSet(userInfo.getId());
String accessToken = RandomUtil.simpleUUID();
@@ -52,7 +86,7 @@ public class LoginController {
cookie.setDomain("zyplayer.com");
cookie.setMaxAge(60 * 60 * 24);
response.addCookie(cookie);
// 再搞一份当前路劲的cookie
// 再搞一份当前域名的cookie
cookie = new Cookie("accessToken", accessToken);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24);
@@ -60,9 +94,77 @@ public class LoginController {
return DocResponseJson.ok();
}
/**
* 退出登录
*/
@PostMapping(value = "/logout")
public DocResponseJson<Object> logout() {
DocUserUtil.logout();
return DocResponseJson.ok();
}
/**
* 域账户注册
*/
private UserInfo ldapAutoRegister(LdapPerson ldapPerson) {
UserInfo userInfo = new UserInfo();
userInfo.setEmail(ldapPerson.getEmail());
userInfo.setPassword("LDAP");
userInfo.setUserName(ldapPerson.getName());
userInfo.setUserNo(ldapPerson.getsAMAccountName());
userInfo.setSex(1);
userInfoService.save(userInfo);
return userInfo;
}
/**
* 是否使用域账号登录
*/
public boolean isUseLdapServer() {
return StringUtils.isNotEmpty(ldapUrls);
}
/**
* 鉴别域账号中是否有该用户
*/
public LdapPerson getUserFromLdap(String username, String password) {
if (StringUtils.endsWithIgnoreCase(username, ldapDomainName)) {
username = username.replaceAll("(?i)" + ldapDomainName, "");
}
String userDn = username + ldapDomainName;
DirContext dirContext = null;
try {
dirContext = ldapTemplate.getContextSource().getContext(userDn, password);
List<LdapPerson> search = ldapTemplate.search(
LdapQueryBuilder.query().where("objectClass").is("person").and("sAMAccountName").is(username),
(AttributesMapper<LdapPerson>) attributes -> {
LdapPerson person = new LdapPerson();
person.setName(this.getAttributeValue(attributes.get("cn")));
person.setsAMAccountName(this.getAttributeValue(attributes.get("sAMAccountName")));
person.setEmail(this.getAttributeValue(attributes.get("userPrincipalName")));
return person;
});
if (CollectionUtils.isNotEmpty(search)) {
return search.get(0);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != dirContext) {
LdapUtils.closeContext(dirContext);
}
}
return null;
}
/**
* 取值
*/
private String getAttributeValue(Attribute attribute) throws NamingException {
if (attribute != null) {
Object obj = attribute.get(0);
return obj == null ? null : obj.toString();
}
return null;
}
}

View File

@@ -0,0 +1,74 @@
/*
* <<
* Davinci
* ==
* Copyright (C) 2016 - 2019 EDP
* ==
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* >>
*
*/
package com.zyplayer.doc.manage.web.manage.param;
public class LdapPerson {
/**
* 姓名
*/
private String name;
/**
* 用户名
*/
private String sAMAccountName;
/**
* 邮箱
*/
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getsAMAccountName() {
return sAMAccountName;
}
public void setsAMAccountName(String sAMAccountName) {
this.sAMAccountName = sAMAccountName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public LdapPerson(String name, String sAMAccountName, String email) {
this.name = name;
this.sAMAccountName = sAMAccountName;
this.email = email;
}
public LdapPerson() {
}
}