提交 fa59db44 编写于 作者: S shiziyuan9527

LDAP 测试登录

上级 4730a63a
package io.metersphere.ldap.controller; package io.metersphere.ldap.controller;
import io.metersphere.base.domain.User; import io.metersphere.base.domain.User;
import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.controller.ResultHolder; import io.metersphere.controller.ResultHolder;
import io.metersphere.controller.request.LoginRequest; import io.metersphere.controller.request.LoginRequest;
import io.metersphere.ldap.service.LdapService; import io.metersphere.ldap.service.LdapService;
import io.metersphere.ldap.domain.LdapInfo; import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.UserService; import io.metersphere.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -18,9 +22,17 @@ public class LdapController { ...@@ -18,9 +22,17 @@ public class LdapController {
private UserService userService; private UserService userService;
@Resource @Resource
private LdapService ldapService; private LdapService ldapService;
@Resource
private SystemParameterService systemParameterService;
@PostMapping(value = "/signin") @PostMapping(value = "/signin")
public ResultHolder login(@RequestBody LoginRequest request) { public ResultHolder login(@RequestBody LoginRequest request) {
String isOpen = systemParameterService.getValue(ParamConstants.LDAP.OPEN.getValue());
if (StringUtils.isBlank(isOpen) || StringUtils.equals(Boolean.FALSE.toString(), isOpen)) {
MSException.throwException("LDAP 认证未启用!");
}
ldapService.authenticate(request); ldapService.authenticate(request);
SecurityUtils.getSubject().getSession().setAttribute("authenticate", "ldap"); SecurityUtils.getSubject().getSession().setAttribute("authenticate", "ldap");
...@@ -45,9 +57,14 @@ public class LdapController { ...@@ -45,9 +57,14 @@ public class LdapController {
return userService.login(request); return userService.login(request);
} }
@PostMapping("/connect") @PostMapping("/test/connect")
public void testConnect(@RequestBody LdapInfo ldapInfo) { public void testConnect(@RequestBody LdapInfo ldapInfo) {
ldapService.testConnect(ldapInfo); ldapService.testConnect(ldapInfo);
} }
@PostMapping("/test/login")
public void testLogin(@RequestBody LoginRequest request) {
ldapService.authenticate(request);
}
} }
...@@ -105,9 +105,12 @@ public class PersonRepoImpl implements PersonRepo { ...@@ -105,9 +105,12 @@ public class PersonRepoImpl implements PersonRepo {
String url = service.getValue(ParamConstants.LDAP.URL.getValue()); String url = service.getValue(ParamConstants.LDAP.URL.getValue());
String dn = service.getValue(ParamConstants.LDAP.DN.getValue()); String dn = service.getValue(ParamConstants.LDAP.DN.getValue());
String ou = service.getValue(ParamConstants.LDAP.OU.getValue()); String ou = service.getValue(ParamConstants.LDAP.OU.getValue());
String credentials = EncryptUtils.aesDecrypt(service.getValue(ParamConstants.LDAP.PASSWORD.getValue())).toString(); String password = service.getValue(ParamConstants.LDAP.PASSWORD.getValue());
preConnect(url, dn, ou, password);
String credentials = EncryptUtils.aesDecrypt(password).toString();
preConnect(url, dn, ou, credentials);
LdapContextSource sourceLdapCtx = new LdapContextSource(); LdapContextSource sourceLdapCtx = new LdapContextSource();
sourceLdapCtx.setUrl(url); sourceLdapCtx.setUrl(url);
......
...@@ -27,33 +27,58 @@ ...@@ -27,33 +27,58 @@
<div> <div>
<el-button type="primary" size="small" :disabled="!show" @click="testConnection">测试连接</el-button> <el-button type="primary" size="small" :disabled="!show" @click="testConnection">测试连接</el-button>
<el-button type="primary" size="small" :disabled="!show">测试登录</el-button> <el-button type="primary" size="small" :disabled="!show" @click="testLogin">测试登录</el-button>
<el-button v-if="showEdit" size="small" @click="edit">编辑</el-button> <el-button v-if="showEdit" size="small" @click="edit">编辑</el-button>
<el-button type="success" v-if="showSave" size="small" @click="save('form')">保存</el-button> <el-button type="success" v-if="showSave" size="small" @click="save('form')">保存</el-button>
<el-button type="info" v-if="showCancel" size="small" @click="cancel">取消</el-button> <el-button type="info" v-if="showCancel" size="small" @click="cancel">取消</el-button>
</div> </div>
<el-dialog title="测试登录" :visible.sync="loginVisible" width="30%" destroy-on-close v-loading="result.loading">
<el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" label-width="80px">
<el-form-item label="用户名" prop="username">
<el-input v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名"/>
</el-form-item>
<el-form-item label="密码" prop="password" >
<el-input v-model="loginForm.password" autocomplete="new-password" placeholder="请输入密码" show-password/>
</el-form-item>
</el-form>
<span slot="footer">
<ms-dialog-footer
@cancel="loginVisible = false"
@confirm="login('loginForm')"/>
</span>
</el-dialog>
</el-card> </el-card>
</div> </div>
</template> </template>
<script> <script>
import MsDialogFooter from "../../common/components/MsDialogFooter";
export default { export default {
name: "LdapSetting", name: "LdapSetting",
components: {
MsDialogFooter
},
data() { data() {
return { return {
form: { form: {open: false},
open: false loginForm: {},
},
result: {}, result: {},
show: true, show: true,
showEdit: true, showEdit: true,
showSave: false, showSave: false,
showCancel: false, showCancel: false,
loginVisible: false,
rules: { rules: {
url: {required: true, message: '请输入LDAP地址', trigger: ['change']}, url: {required: true, message: '请输入LDAP地址', trigger: ['change','blur']},
dn: {required: true, message: '请输入DN', trigger: ['change']}, dn: {required: true, message: '请输入DN', trigger: ['change','blur']},
password: {required: true, message: '请输入密码', trigger: ['change']}, password: {required: true, message: '请输入密码', trigger: ['change','blur']},
ou: {required: true, message: '请输入OU', trigger: ['change']}, ou: {required: true, message: '请输入OU', trigger: ['change','blur']},
},
loginFormRules: {
username: {required: true, message: '请输入用户名', trigger: 'blur'},
password: {required: true, message: '请输入密码', trigger: 'blur'}
} }
} }
}, },
...@@ -65,6 +90,9 @@ ...@@ -65,6 +90,9 @@
this.result = this.$get("/system/ldap/info", response => { this.result = this.$get("/system/ldap/info", response => {
this.form = response.data; this.form = response.data;
this.form.open = this.form.open === 'true' ? true : false; this.form.open = this.form.open === 'true' ? true : false;
this.$nextTick(() => {
this.$refs.form.clearValidate();
})
}) })
}, },
edit() { edit() {
...@@ -81,10 +109,44 @@ ...@@ -81,10 +109,44 @@
this.init(); this.init();
}, },
testConnection() { testConnection() {
this.result = this.$post("/ldap/connect", this.form, response => { if (!this.checkParam()) {
return false;
}
this.result = this.$post("/ldap/test/connect", this.form, response => {
this.$success("连接成功!") this.$success("连接成功!")
}) })
}, },
testLogin() {
if (!this.checkParam()) {
return false;
}
if (!this.form.ou) {
this.$warning("LDAP OU不能为空!");
return false;
}
this.loginForm = {};
this.loginVisible = true;
},
checkParam() {
if (!this.form.url) {
this.$warning("LDAP 地址不能为空!");
return false;
}
if (!this.form.dn) {
this.$warning("LDAP DN不能为空!");
return false;
}
if (!this.form.password) {
this.$warning("LDAP 密码不能为空!");
return false;
}
return true;
},
save(form) { save(form) {
let param = [ let param = [
...@@ -111,6 +173,17 @@ ...@@ -111,6 +173,17 @@
return false; return false;
} }
}) })
},
login(form) {
this.$refs[form].validate(valid => {
if (valid) {
this.result = this.$post("/ldap/test/login", this.loginForm, response => {
this.$success("登录成功")
});
} else {
return false;
}
})
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册