Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 3e24474

Browse files
authored
feat: add support for Casdoor authentication (#39)
1 parent 23862b4 commit 3e24474

File tree

9 files changed

+85
-2
lines changed

9 files changed

+85
-2
lines changed

backend/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@
144144
<version>1.9.6</version>
145145
</dependency>
146146

147+
<dependency>
148+
<groupId>org.casbin</groupId>
149+
<artifactId>casdoor-spring-boot-starter</artifactId>
150+
<version>1.3.0</version>
151+
</dependency>
152+
147153
<dependency>
148154
<groupId>org.apache.iotdb</groupId>
149155
<artifactId>iotdb-session</artifactId>

backend/src/main/java/org/apache/iotdb/admin/controller/UserController.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io.jsonwebtoken.Claims;
3434
import io.swagger.annotations.Api;
3535
import io.swagger.annotations.ApiOperation;
36+
import org.casbin.casdoor.entity.CasdoorUser;
37+
import org.casbin.casdoor.service.CasdoorAuthService;
3638
import org.slf4j.Logger;
3739
import org.slf4j.LoggerFactory;
3840
import org.springframework.beans.factory.annotation.Autowired;
@@ -53,6 +55,8 @@ public class UserController {
5355

5456
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
5557

58+
@Autowired private CasdoorAuthService casdoorAuthService;
59+
5660
@PostMapping("/login")
5761
@ApiOperation("login")
5862
public BaseVO<ConnectionVO> login(
@@ -71,6 +75,35 @@ public BaseVO<ConnectionVO> login(
7175
return BaseVO.success("Login successful", connectionVO);
7276
}
7377

78+
@PostMapping("/getCasdoorUrl")
79+
@ApiOperation("Get Casdoor Url")
80+
public BaseVO<String> getCasdoorUrl(HttpServletRequest request, HttpServletResponse response)
81+
throws BaseException {
82+
String origin = request.getParameter("origin");
83+
String url = casdoorAuthService.getSigninUrl(origin);
84+
return BaseVO.success("Get Url successful", url);
85+
}
86+
87+
@PostMapping("/loginWithCasdoor")
88+
@ApiOperation("loginWithCasdoor")
89+
public BaseVO<ConnectionVO> loginWithCasdoor(
90+
@RequestParam("code") String code,
91+
@RequestParam("state") String state,
92+
HttpServletResponse response)
93+
throws BaseException {
94+
String token = casdoorAuthService.getOAuthToken(code, state);
95+
CasdoorUser casdoorUser = casdoorAuthService.parseJwtToken(token);
96+
User user = new User();
97+
user.setId(casdoorUser.getRanking());
98+
user.setName(casdoorUser.getName());
99+
int userId = user.getId();
100+
String name = user.getName();
101+
List<ConnVO> connVOs = connectionService.getAllConnections(userId);
102+
ConnectionVO connectionVO = new ConnectionVO(connVOs, userId, name);
103+
response.addHeader("Authorization", JJwtTool.generateToken(user));
104+
return BaseVO.success("Login successful", connectionVO);
105+
}
106+
74107
@PostMapping("/save")
75108
@ApiOperation("Create user (not used)")
76109
public BaseVO save(@RequestBody User user) throws BaseException {

backend/src/main/resources/application.properties

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@
1818
#
1919

2020
# Designate the configuration file
21-
spring.profiles.active=dev
21+
spring.profiles.active=dev
22+
23+
casdoor.endpoint =
24+
casdoor.clientId =
25+
casdoor.clientSecret =
26+
casdoor.certificate =
27+
casdoor.organizationName =
28+
casdoor.applicationName =

frontend/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VUE_APP_CASDOOR=false

frontend/src/i18n/cn.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const cn = {
8686
placeholderPassword: '请输入密码',
8787
forgetPassWord: '忘记密码',
8888
signIn: '登录',
89+
signInWithCasdoor: 'Casdoor登录',
8990
forgetPassword: '忘记密码',
9091
forgetPasswordTip: '请联系系统管理员 WeChat:loveher147',
9192
accountEmptyTip: '账号不能为空',

frontend/src/i18n/de.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const de = {
8585
placeholderPassword: 'Passwort eingeben',
8686
forgetPassWord: 'Passwort vergessen',
8787
signIn: 'Anmelden',
88+
signInWithCasdoor: 'Loggen Sie sich mit casdoor ein',
8889
forgetPassword: 'Passwort vergessen',
8990
forgetPasswordTip: 'Bitte kontaktieren Sie Ihren Administrator WeChat:loveher147',
9091
accountEmptyTip: 'Benutzername darf nicht leer sein',

frontend/src/i18n/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const en = {
8585
placeholderPassword: 'Please Input Password',
8686
forgetPassWord: 'Forget Password',
8787
signIn: 'Sign In',
88+
signInWithCasdoor: 'Sign In With Casdoor',
8889
forgetPassword: 'Forget Password',
8990
forgetPasswordTip: 'Please Contact System Administrator WeChat:loveher147',
9091
accountEmptyTip: 'Account Can Not Be Empty',

frontend/src/util/axios.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ElMessage } from 'element-plus';
2222
import router from '../router';
2323

2424
const instance = axios.create({});
25-
const headerUrls = ['/api/login', '/api/downloadFile/template', '/api/downloadQueryLogFile'];
25+
const headerUrls = ['/api/login', '/api/loginWithCasdoor', '/api/downloadFile/template', '/api/downloadQueryLogFile'];
2626
const exportUrl = '/exportData';
2727
const downUrl = '/downloadFile';
2828
instance.defaults.withCredentials = true;

frontend/src/views/Login/index.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<el-button class="submit-btn" type="primary" @click="submitForm('ruleForm')">{{ $t('loginPage.signIn') }}</el-button>
6161
</el-form-item>
6262
</el-form>
63+
<el-form-item v-if="casdoorSwitch === 'true'">
64+
<el-button class="submit-btn" type="primary" @click="getLoginUrl()">{{ $t('loginPage.signInWithCasdoor') }}</el-button>
65+
</el-form-item>
6366
</div>
6467
</div>
6568
</div>
@@ -90,6 +93,7 @@ export default {
9093
const formNameRef = ref(null);
9194
const dialogVisible = ref(false);
9295
const { t } = useI18n();
96+
const casdoorSwitch = process.env.VUE_APP_CASDOOR;
9397
const ruleForm = reactive({
9498
account: '',
9599
passport: '',
@@ -145,6 +149,7 @@ export default {
145149
// if (store.state.isLogin) {
146150
// router.push({ name: "Root" });
147151
// }
152+
LoginWithCasdoor();
148153
});
149154
150155
const submitForm = () => {
@@ -164,6 +169,31 @@ export default {
164169
});
165170
};
166171
172+
const getLoginUrl = () => {
173+
axios.post('/getCasdoorUrl', {}, { params: { origin: window.location.origin } }).then((res) => {
174+
window.location.href = res.data;
175+
});
176+
};
177+
178+
const LoginWithCasdoor = () => {
179+
const url = window.document.location.href;
180+
const u = new URL(url);
181+
let codes = u.searchParams.get('code');
182+
let state = u.searchParams.get('state');
183+
if (codes != null && state != null) {
184+
axios.post('/loginWithCasdoor', {}, { params: { code: codes, state: state } }).then((res) => {
185+
if (res?.data?.code === '0') {
186+
localStorage.setItem('authorization', res?.headers?.authorization);
187+
store.commit('setLogin', true);
188+
store.commit('setUserInfo', res.data || {});
189+
router.push({ name: 'Root' });
190+
} else {
191+
ElMessage.error(t(`loginPage.loginErrorTip`));
192+
}
193+
});
194+
}
195+
};
196+
167197
const showDialog = () => {
168198
dialogVisible.value = true;
169199
};
@@ -177,6 +207,9 @@ export default {
177207
rules,
178208
submitForm,
179209
showDialog,
210+
getLoginUrl,
211+
LoginWithCasdoor,
212+
casdoorSwitch,
180213
};
181214
},
182215
components: { ElForm, ElFormItem, ElInput, ElButton, ElDialog, ElDropdown, ElDropdownMenu, ElDropdownItem },

0 commit comments

Comments
 (0)