1
0
mirror of https://github.com/FatttSnake/Pinnacle-OA.git synced 2026-04-05 23:11:24 +08:00

Fixed token to large error

This commit is contained in:
2023-05-23 02:29:36 +08:00
parent f87df23ea1
commit bd44024c29
7 changed files with 40 additions and 28 deletions

View File

@@ -33,6 +33,12 @@ public class UserController {
this.userService = userService; this.userService = userService;
} }
@GetMapping("/info")
@Operation(summary = "获取当前用户信息")
public ResponseResult<User> getInfo() {
return ResponseResult.databaseSelectSuccess(userService.getInfo());
}
@GetMapping @GetMapping
@PreAuthorize("hasAnyAuthority('system:user:all', 'system:user:add', 'system:user:modify')") @PreAuthorize("hasAnyAuthority('system:user:all', 'system:user:add', 'system:user:modify')")
@Operation(summary = "获取所有用户(权限管理相关)") @Operation(summary = "获取所有用户(权限管理相关)")

View File

@@ -15,6 +15,8 @@ import java.util.List;
*/ */
public interface IUserService extends IService<User> { public interface IUserService extends IService<User> {
User getInfo();
List<User> getAllUser(); List<User> getAllUser();
User getUser(long id); User getUser(long id);

View File

@@ -11,6 +11,7 @@ import com.cfive.pinnacle.mapper.permission.MenuMapper;
import com.cfive.pinnacle.mapper.permission.OperationMapper; import com.cfive.pinnacle.mapper.permission.OperationMapper;
import com.cfive.pinnacle.service.IUserService; import com.cfive.pinnacle.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfive.pinnacle.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -72,6 +73,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
this.passwordEncoder = passwordEncoder; this.passwordEncoder = passwordEncoder;
} }
@Override
public User getInfo() {
return WebUtil.getLoginUser().getUser();
}
@Override @Override
public List<User> getAllUser() { public List<User> getAllUser() {
List<User> users = userMapper.getAll(); List<User> users = userMapper.getAll();

View File

@@ -5,8 +5,6 @@ import com.cfive.pinnacle.entity.permission.LoginUser;
import com.cfive.pinnacle.service.permission.ILoginService; import com.cfive.pinnacle.service.permission.ILoginService;
import com.cfive.pinnacle.utils.JwtUtil; import com.cfive.pinnacle.utils.JwtUtil;
import com.cfive.pinnacle.utils.RedisCache; import com.cfive.pinnacle.utils.RedisCache;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -43,13 +41,7 @@ public class LoginServiceImpl implements ILoginService {
LoginUser loginUser = (LoginUser) authentication.getPrincipal(); LoginUser loginUser = (LoginUser) authentication.getPrincipal();
loginUser.getUser().setPasswd(""); loginUser.getUser().setPasswd("");
String userId = loginUser.getUser().getId().toString(); String userId = loginUser.getUser().getId().toString();
String jwt; String jwt = JwtUtil.createJWT(userId);
try {
jwt = JwtUtil.createJWT(new ObjectMapper().writeValueAsString(loginUser.getUser()));
} catch (JsonProcessingException e) {
jwt = JwtUtil.createJWT(userId);
}
HashMap<String, String> hashMap = new HashMap<>(); HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("token", jwt); hashMap.put("token", jwt);

View File

@@ -223,13 +223,13 @@ export default {
setLocalStorage('menuCollapsed', this.isCollapsed.toString()) setLocalStorage('menuCollapsed', this.isCollapsed.toString())
} }
}, },
mounted() { async mounted() {
this.username = getUsername() this.username = await getUsername()
const allRoutes = _.cloneDeep( const allRoutes = _.cloneDeep(
_.filter(_.get(this.$router, 'options.routes[0].children'), 'meta.title') _.filter(_.get(this.$router, 'options.routes[0].children'), 'meta.title')
) )
const user = getUser() const user = await getUser()
const menus = user.menus const menus = user.menus
this.routes = allRoutes.filter((level1) => { this.routes = allRoutes.filter((level1) => {
if (level1.meta.requiresAuth) { if (level1.meta.requiresAuth) {

View File

@@ -50,7 +50,7 @@ const router = createRouter({
] ]
}) })
router.beforeEach((to, from, next) => { router.beforeEach(async (to, from, next) => {
if (to.matched.length === 0) { if (to.matched.length === 0) {
from.path !== '' ? next({ path: from.path }) : next('/') from.path !== '' ? next({ path: from.path }) : next('/')
} else { } else {
@@ -64,7 +64,7 @@ router.beforeEach((to, from, next) => {
next('/') next('/')
} else { } else {
if (to.meta.requiresAuth === true) { if (to.meta.requiresAuth === true) {
const user = getUser() const user = await getUser()
const menus = user.menus const menus = user.menus
for (const menu of menus) { for (const menu of menus) {
if (menu.url === '/') continue if (menu.url === '/') continue

View File

@@ -1,8 +1,7 @@
import type { Captcha } from './common' import type { Captcha } from './common'
import { clearLocalStorage, getCaptcha, getLocalStorage, getToken } from './common' import { clearLocalStorage, getCaptcha, getLocalStorage, setLocalStorage } from './common'
import { TOKEN_NAME } from '@/constants/Common.constants' import { DATABASE_SELECT_OK, TOKEN_NAME } from '@/constants/Common.constants'
import request from '@/services' import request from '@/services'
import jwtDecode, { type JwtPayload } from 'jwt-decode'
import _ from 'lodash' import _ from 'lodash'
let captcha: Captcha let captcha: Captcha
@@ -21,20 +20,27 @@ function getLoginStatus(): boolean {
return getLocalStorage(TOKEN_NAME) != null return getLocalStorage(TOKEN_NAME) != null
} }
function getUser(): any { async function getUser(): Promise<any> {
const token = getToken() if (getLocalStorage('userInfo') !== null) {
return JSON.parse(getLocalStorage('userInfo') as string)
if (token === null) {
logout()
return ''
} }
return await requestUser()
const jwtPayload: JwtPayload = jwtDecode(token)
return JSON.parse(jwtPayload.sub ?? '')
} }
function getUsername(): string { async function requestUser(): Promise<any> {
const user = getUser() let user
await request.get('/user/info').then((res) => {
const response = res.data
if (response.code === DATABASE_SELECT_OK) {
user = response.data
setLocalStorage('userInfo', JSON.stringify(user))
}
})
return user
}
async function getUsername(): Promise<string> {
const user = await getUser()
return user.staff != null return user.staff != null
? `${_.toString(user.staff.lastName)}${_.toString(user.staff.firstName)}` ? `${_.toString(user.staff.lastName)}${_.toString(user.staff.firstName)}`