feat: 登录

This commit is contained in:
fit2cloud-chenyw 2021-02-19 17:05:11 +08:00
parent 97960a6f0b
commit 9c477fb463
13 changed files with 64 additions and 168 deletions

View File

@ -35,12 +35,12 @@ public class UserApi {
throw new RuntimeException("没有该用户!");
}
String pwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, password);
String realpwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, realPwd);
if (!StrUtil.equals(pwd, realpwd)){
String realPass = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, realPwd);
if (!StrUtil.equals(pwd, realPass)){
throw new RuntimeException("密码错误!");
}
Map<String,Object> result = new HashMap<>();
result.put("token", JWTUtil.sign(username, realpwd));
result.put("token", JWTUtil.sign(username, realPass));
return result;
}

View File

@ -3,6 +3,7 @@ package com.fit2cloud.commons.auth.config;
import com.fit2cloud.commons.auth.bean.UserBean;
import com.fit2cloud.commons.auth.service.UserService;
import com.fit2cloud.commons.auth.util.JWTUtil;
import com.fit2cloud.commons.auth.util.RsaUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
@ -67,8 +68,13 @@ public class F2cRealm extends AuthorizingRealm {
if (userBean == null) {
throw new AuthenticationException("User didn't existed!");
}
if (! JWTUtil.verify(token, username, userBean.getPassword())) {
String pwd = null;
try {
pwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, userBean.getPassword());
} catch (Exception e) {
e.printStackTrace();
}
if (! JWTUtil.verify(token, username, pwd)) {
throw new AuthenticationException("Username or password error");
}

View File

@ -15,7 +15,7 @@ public interface ExtUserMapper {
@Select("select password from sys_user where username = #{userName,jdbcType=VARCHAR} ")
String getPassword(String userName);
@Select("select role_id from sys_users_roles where username = #{userName,jdbcType=VARCHAR} ")
@Select("select role_id from sys_users_roles sur left join sys_user su on su.user_id = sur.user_id where su.username = #{userName,jdbcType=VARCHAR} ")
List<String> getRole(String userName);
@Select({
@ -23,7 +23,8 @@ public interface ExtUserMapper {
"from sys_users_roles sur ",
"LEFT JOIN sys_roles_menus srm on srm.role_id = sur.role_id ",
"LEFT JOIN sys_menu sm on sm.menu_id = srm.menu_id ",
"where sur.username = #{userName,jdbcType=VARCHAR} "
"LEFT JOIN sys_user su on su.user_id = sur.user_id ",
"where su.username = #{userName,jdbcType=VARCHAR} "
})
List<String> getPermission(String userName);

View File

@ -2,4 +2,4 @@
ENV = 'development'
# base api, e.g., '/dev'
VUE_APP_BASE_API = ''
VUE_APP_BASE_API = 'http://localhost:8081/'

View File

@ -41,7 +41,7 @@ const users = {
module.exports = [
// user login
{
url: '/samples/user-token/login',
url: '/samples/user/login',
type: 'post',
response: config => {
const {username} = config.body
@ -57,7 +57,7 @@ module.exports = [
// get user info
{
url: '/samples/user-token/info',
url: '/samples/user/info',
type: 'get',
response: (config) => {
let token = config.headers[TOKEN_KEY]

View File

@ -1,21 +0,0 @@
/* 前后端分离的登录方式 */
import {get, post, put} from "@/plugins/request"
export function login(data) {
return post("/login", data)
}
export function logout() {
return post("/logout")
}
export function getCurrentUser() {
return get("/info")
}
export function updateInfo(data) {
return put("/update", data)
}

View File

@ -1,24 +1,20 @@
/* 前后端分离的登录方式 */
/* 前后端分离的登录方式 */
import {get, post, put} from "@/plugins/request"
export function login(data) {
return post("/samples/user/login", data)
return post("/login", data)
}
export function logout() {
return post("/samples/user/logout")
}
export function isLogin() {
return get("/samples/user/is-login")
return post("/logout")
}
export function getCurrentUser() {
return get("/samples/user/current")
return get("/info")
}
export function updateInfo(id, data) {
return put("/samples/user/info/update/" + id, data)
export function updateInfo(data) {
return put("/update", data)
}

View File

@ -43,6 +43,7 @@
</template>
<script>
import { encrypt } from '@/utils/rsaEncrypt'
export default {
name: "Login",
data() {
@ -96,8 +97,13 @@ export default {
submit(form) {
this.$refs[form].validate((valid) => {
if (valid) {
const user = {
username: this.form.username,
password: this.form.password
}
user.password = encrypt(user.password)
this.loading = true;
this.$store.dispatch('user/login', this.form).then(() => {
this.$store.dispatch('user/login', user).then(() => {
this.$router.push({path: this.redirect || '/', query: this.otherQuery})
this.loading = false
}).catch(error => {

View File

@ -14,12 +14,12 @@ const generateRoutes = async (to, from, next) => {
next()
} else {
try {
const {roles} = await store.dispatch('user-token/getCurrentUser')
const {roles} = await store.dispatch('user/getCurrentUser')
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
router.addRoutes(accessRoutes)
next({...to, replace: true})
} catch (error) {
await store.dispatch('user-token/logout')
await store.dispatch('user/logout')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
@ -29,7 +29,6 @@ const generateRoutes = async (to, from, next) => {
// 路由前置钩子,根据实际需求修改
router.beforeEach(async (to, from, next) => {
NProgress.start()
// const isLogin = await store.dispatch('user-token/isLogin') // 或者user-token/isLogin
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {

View File

@ -6,5 +6,6 @@ const getters = {
roles: state => state.user.roles,
permission_routes: state => state.permission.routes,
license: state => state.license,
token: state => state.user.token,
}
export default getters

View File

@ -1,97 +0,0 @@
import {login, getCurrentUser, updateInfo, logout} from '@/api/user-token'
import {resetRouter} from '@/router'
import {getToken, setToken, removeToken} from '@/utils/token'
import {getLanguage, setLanguage} from "@/i18n";
/* 前后端不分离的登录办法*/
const state = {
token: getToken(),
name: "",
language: getLanguage(),
roles: []
}
const mutations = {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
},
SET_LANGUAGE: (state, language) => {
state.language = language
setLanguage(language)
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
const actions = {
login({commit}, userInfo) {
const {username, password} = userInfo
return new Promise((resolve, reject) => {
login({username: username.trim(), password: password}).then(response => {
let token = response.data
commit('SET_TOKEN', token)
setToken(token)
resolve(response)
}).catch(error => {
reject(error)
})
})
},
isLogin({commit}) {
return new Promise((resolve, reject) => {
let token = getToken()
if (token) {
commit('SET_TOKEN', token);
resolve(true)
} else {
reject(false)
}
});
},
getCurrentUser({commit}) {
return new Promise((resolve, reject) => {
getCurrentUser().then(response => {
const {name, roles, language} = response.data
commit('SET_NAME', name)
commit('SET_ROLES', roles)
commit('SET_LANGUAGE', language)
resolve(response.data)
}).catch(error => {
reject(error)
})
});
},
setLanguage({commit, state}, language) {
commit('SET_LANGUAGE', language)
return new Promise((resolve, reject) => {
updateInfo(state.id, {language: language}).then(response => {
resolve(response)
}).catch(error => {
reject(error)
})
})
},
logout({commit}) {
logout().then(() => {
commit('SET_TOKEN', "");
commit('SET_ROLES', [])
removeToken()
resetRouter()
})
},
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -1,21 +1,25 @@
/* 前后端不分离的登录方式*/
import {login, isLogin, getCurrentUser, updateInfo, logout} from '@/api/user'
import {login, getCurrentUser, updateInfo, logout} from '@/api/user'
import {resetRouter} from '@/router'
import {getToken, setToken, removeToken} from '@/utils/token'
import {getLanguage, setLanguage} from "@/i18n";
const state = {
login: false,
name: "",
language: getLanguage(),
roles: []
/* 前后端不分离的登录办法*/
const getDefaultState = () => {
return {
token: getToken(),
name: "",
language: getLanguage(),
roles: []
}
}
const state = getDefaultState()
const mutations = {
LOGIN: (state) => {
state.login = true
},
LOGOUT: (state) => {
state.login = false
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
@ -33,8 +37,10 @@ const actions = {
login({commit}, userInfo) {
const {username, password} = userInfo
return new Promise((resolve, reject) => {
login({username: username.trim(), password: password}).then(response => {
commit('LOGIN')
login({username: username.trim(), password: password}).then(response => {
let token = response.data.token
commit('SET_TOKEN', token)
setToken(token)
resolve(response)
}).catch(error => {
reject(error)
@ -43,17 +49,14 @@ const actions = {
},
isLogin({commit}) {
return new Promise((resolve) => {
if (state.login) {
return new Promise((resolve, reject) => {
let token = getToken()
if (token) {
commit('SET_TOKEN', token);
resolve(true)
return;
} else {
reject(false)
}
isLogin().then(() => {
commit('LOGIN')
resolve(true)
}).catch(() => {
resolve(false)
})
});
},
@ -84,14 +87,15 @@ const actions = {
logout({commit}) {
logout().then(() => {
commit('LOGOUT')
commit('SET_TOKEN', "");
commit('SET_ROLES', [])
removeToken()
resetRouter()
})
},
}
export default {
export default {
namespaced: true,
state,
mutations,

View File

@ -27,7 +27,8 @@ module.exports = {
// }
// },
configureWebpack: {
devtool: 'source-map',
// devtool: 'source-map',
devtool: 'eval-source-map',
resolve: {
alias: {
'@': resolve('src')