Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持OAuth2 单点登录 #751

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions config/application-config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ spring:
username: { USERNAME }
password: { PASSWORD }

# security:
# oauth2:
# enable: true
# client:
# registration:
# cas:
# provider: cas
# client-id: "xxxxx"
# client-name: "Sign in with CAS"
# client-secret: "xxx"
# authorization-grant-type: authorization_code
# client-authentication-method: post
# redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
# scope: userinfo
# provider:
# cas:
# authorization-uri: https://cas.xxx.com/cas/oauth2.0/authorize
# token-uri: https://cas.xxx.com/cas/oauth2.0/accessToken
# user-info-uri: https://cas.xxx.com/cas/oauth2.0/profile
# user-name-attribute: id
# userMapping:
# email: "attributes.email"
# name: "attributes.name"
# avatar: "attributes.avatar"

# mail config

# mail:
Expand Down Expand Up @@ -38,10 +63,10 @@ spring:
server:
port: { PORT }
address: { IP }

# 开启 gzip 压缩,加快请求和响应速度
compression:
enabled: true
enabled: true
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*


Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/datart/core/common/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static String getWebRootURL() {
}

public static String getApiPrefix() {
return getProperty("datart.path-prefix");
return getProperty("datart.server.path-prefix");
}

public static String getTokenSecret() {
Expand Down
51 changes: 40 additions & 11 deletions frontend/src/app/pages/LoginPage/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
* limitations under the License.
*/

import { Button, Form, Input } from 'antd';
import { AuthForm } from 'app/components';
import {Button, Form, Input} from 'antd';
import {AuthForm} from 'app/components';
import usePrefixI18N from 'app/hooks/useI18NPrefix';
import { selectLoggedInUser, selectLoginLoading } from 'app/slice/selectors';
import { login } from 'app/slice/thunks';
import React, { useCallback, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useHistory } from 'react-router-dom';
import {selectLoggedInUser, selectLoginLoading, selectOauth2Clients, selectVersion} from 'app/slice/selectors';
import {getOauth2Clients, login, tryOauth} from 'app/slice/thunks';
import React, {useCallback, useEffect, useState} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {Link, useHistory} from 'react-router-dom';
import styled from 'styled-components/macro';
import {
BORDER_RADIUS,
LINE_HEIGHT_ICON_LG,
SPACE_MD,
} from 'styles/StyleConstants';
import { getToken } from 'utils/auth';
import {getToken} from 'utils/auth';
import {editDashBoardInfoActions} from "../DashBoardPage/pages/BoardEditor/slice";

export function LoginForm() {
const [switchUser, setSwitchUser] = useState(false);
Expand All @@ -42,11 +43,24 @@ export function LoginForm() {
const logged = !!getToken();
const t = usePrefixI18N('login');
const tg = usePrefixI18N('global');
const oauth2Clients = useSelector(selectOauth2Clients);

const toApp = useCallback(() => {
history.replace('/');
}, [history]);

useEffect(() => {
dispatch(
getOauth2Clients(),
);
}, [dispatch]);

useEffect(() => {
dispatch(
tryOauth(),
);
}, [dispatch]);

const onLogin = useCallback(
values => {
dispatch(
Expand All @@ -65,6 +79,10 @@ export function LoginForm() {
setSwitchUser(true);
}, []);

let Oauth2BtnList = oauth2Clients.map((client) => {
return (<Oauth2Button key={client.value} href={client.value}>{client.name}</Oauth2Button>)
});

return (
<AuthForm>
{logged && !switchUser ? (
Expand All @@ -89,7 +107,7 @@ export function LoginForm() {
},
]}
>
<Input placeholder={t('username')} size="large" />
<Input placeholder={t('username')} size="large"/>
</Form.Item>
<Form.Item
name="password"
Expand All @@ -100,7 +118,7 @@ export function LoginForm() {
},
]}
>
<Input placeholder={t('password')} type="password" size="large" />
<Input placeholder={t('password')} type="password" size="large"/>
</Form.Item>
<Form.Item className="last" shouldUpdate>
{() => (
Expand All @@ -112,7 +130,7 @@ export function LoginForm() {
disabled={
loading ||
// !form.isFieldsTouched(true) ||
!!form.getFieldsError().filter(({ errors }) => errors.length)
!!form.getFieldsError().filter(({errors}) => errors.length)
.length
}
block
Expand All @@ -125,6 +143,7 @@ export function LoginForm() {
<LinkButton to="/forgetPassword">{t('forgotPassword')}</LinkButton>
<LinkButton to="/register">{t('register')}</LinkButton>
</Links>
{Oauth2BtnList}
</Form>
)}
</AuthForm>
Expand All @@ -135,6 +154,16 @@ const Links = styled.div`
display: flex;
`;

const Oauth2Button = styled.a`
display: block;
background-color: blue;
text-align: center;
color: #fff;
font-weight: bold;
line-height: 36px;
height: 36px;
`;

const LinkButton = styled(Link)`
flex: 1;
line-height: ${LINE_HEIGHT_ICON_LG};
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/app/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { useInjectReducer } from 'utils/@reduxjs/injectReducer';
import {
getOauth2Clients,
getSystemInfo,
getUserInfoByToken,
login,
Expand All @@ -37,6 +38,7 @@ export const initialState: AppState = {
registerLoading: false,
saveProfileLoading: false,
modifyPasswordLoading: false,
oauth2Clients:[],
};

const slice = createSlice({
Expand Down Expand Up @@ -116,6 +118,13 @@ const slice = createSlice({
builder.addCase(getSystemInfo.fulfilled, (state, action) => {
state.systemInfo = action.payload;
});

builder.addCase(getOauth2Clients.fulfilled, (state, action) => {
state.oauth2Clients =action.payload.map(x=>({
name:Object.keys(x)[0],
value:x[Object.keys(x)[0]]
}));
});
},
});

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/app/slice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ export const selectModifyPasswordLoading = createSelector(
[selectDomain],
appState => appState.modifyPasswordLoading,
);

export const selectOauth2Clients = createSelector(
[selectDomain],
appState => appState.oauth2Clients,
);
72 changes: 53 additions & 19 deletions frontend/src/app/slice/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* limitations under the License.
*/

import { createAsyncThunk } from '@reduxjs/toolkit';
import { StorageKeys } from 'globalConstants';
import { removeToken, setToken, setTokenExpiration } from 'utils/auth';
import { request } from 'utils/request';
import { errorHandle } from 'utils/utils';
import { appActions } from '.';
import {createAsyncThunk} from '@reduxjs/toolkit';
import {StorageKeys} from 'globalConstants';
import {removeToken, setToken, setTokenExpiration} from 'utils/auth';
import {request} from 'utils/request';
import {errorHandle} from 'utils/utils';
import {appActions} from '.';
import {
LoginParams,
LogoutParams,
Expand All @@ -35,9 +35,9 @@ import {

export const login = createAsyncThunk<User, LoginParams>(
'app/login',
async ({ params, resolve }) => {
async ({params, resolve}) => {
try {
const { data } = await request<User>({
const {data} = await request<User>({
url: '/users/login',
method: 'POST',
data: params,
Expand All @@ -54,10 +54,10 @@ export const login = createAsyncThunk<User, LoginParams>(

export const getUserInfoByToken = createAsyncThunk<User, UserInfoByTokenParams>(
'app/getUserInfoByToken',
async ({ token, resolve }) => {
async ({token, resolve}) => {
setToken(token);
try {
const { data } = await request<User>({
const {data} = await request<User>({
url: '/users',
method: 'GET',
});
Expand All @@ -74,7 +74,7 @@ export const getUserInfoByToken = createAsyncThunk<User, UserInfoByTokenParams>(

export const register = createAsyncThunk<null, RegisterParams>(
'app/register',
async ({ data, resolve }) => {
async ({data, resolve}) => {
try {
await request<User>({
url: '/users/register',
Expand Down Expand Up @@ -120,7 +120,7 @@ export const logout = createAsyncThunk<undefined, LogoutParams>(

export const updateUser = createAsyncThunk<null, User>(
'app/updateUser',
async (user, { dispatch }) => {
async (user, {dispatch}) => {
try {
localStorage.setItem(StorageKeys.LoggedInUser, JSON.stringify(user));
dispatch(appActions.updateUser(user));
Expand All @@ -134,9 +134,9 @@ export const updateUser = createAsyncThunk<null, User>(

export const saveProfile = createAsyncThunk<User, SaveProfileParams>(
'app/saveProfile',
async ({ user, resolve }) => {
async ({user, resolve}) => {
const loggedInUser = localStorage.getItem(StorageKeys.LoggedInUser) || '{}';
const merged = { ...JSON.parse(loggedInUser), ...user };
const merged = {...JSON.parse(loggedInUser), ...user};
try {
await request({
url: '/users',
Expand All @@ -153,10 +153,8 @@ export const saveProfile = createAsyncThunk<User, SaveProfileParams>(
},
);

export const modifyAccountPassword = createAsyncThunk<
void,
ModifyPasswordParams
>('app/modifyAccountPassword', async ({ params, resolve }) => {
export const modifyAccountPassword = createAsyncThunk<void,
ModifyPasswordParams>('app/modifyAccountPassword', async ({params, resolve}) => {
try {
await request({
url: '/users/change/password',
Expand All @@ -174,7 +172,7 @@ export const getSystemInfo = createAsyncThunk<SystemInfo>(
'app/getSystemInfo',
async () => {
try {
const { data } = await request<SystemInfo>('/sys/info');
const {data} = await request<SystemInfo>('/sys/info');
// minute -> millisecond
const tokenTimeout = Number(data.tokenTimeout) * 60 * 1000;
setTokenExpiration(tokenTimeout);
Expand All @@ -185,3 +183,39 @@ export const getSystemInfo = createAsyncThunk<SystemInfo>(
}
},
);

export const getOauth2Clients = createAsyncThunk<[]>(
'app/getOauth2Clients',
async () => {
try {
const {data} = await request<[]>({
url: '/tpa/getOauth2Clients',
method: 'GET'
});
return data;
} catch (error) {
errorHandle(error);
throw error;
}
},
);

export const tryOauth = createAsyncThunk<User>(
'app/tryOauth',
async () => {
try {
const {data} = await request<User>({
url: '/tpa/oauth2login',
method: 'POST'
});
localStorage.setItem(StorageKeys.LoggedInUser, JSON.stringify(data));
setTimeout(() => {
window.location.href = '/';
});
return data;
} catch (error) {
errorHandle(error);
throw error;
}
},
);
1 change: 1 addition & 0 deletions frontend/src/app/slice/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface AppState {
registerLoading: boolean;
saveProfileLoading: boolean;
modifyPasswordLoading: boolean;
oauth2Clients: Array<{name:string,value:string}>;
}

export interface User {
Expand Down
17 changes: 17 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>datart</groupId>
<artifactId>datart-core</artifactId>
Expand Down Expand Up @@ -246,6 +257,9 @@
<argument>bootstrap</argument>
<argument>--registry=https://registry.npm.taobao.org</argument>
</arguments>
<environmentVariables>
<NODE_OPTIONS>--openssl-legacy-provider</NODE_OPTIONS>
</environmentVariables>
<workingDirectory>${project.parent.basedir}/frontend</workingDirectory>
</configuration>
</execution>
Expand All @@ -262,6 +276,9 @@
<argument>run</argument>
<argument>build:all</argument>
</arguments>
<environmentVariables>
<NODE_OPTIONS>--openssl-legacy-provider</NODE_OPTIONS>
</environmentVariables>
<workingDirectory>${project.parent.basedir}/frontend</workingDirectory>
</configuration>
</execution>
Expand Down
Loading