Skip to content

Commit

Permalink
授权成功后的重定向问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuXS committed Oct 13, 2017
1 parent f18fc8f commit c6359ac
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ public ShiroFilterFactoryBean shiroFilterFactoryBean(){
Map<String,String> filterChainDefinitionManager = new LinkedHashMap<String,String>();
filterChainDefinitionManager.put("/logout","logout");
filterChainDefinitionManager.put("/userInfo","authc");
//filterChainDefinitionManager.put("/jobs/**","perms['WORDCOUNT:CREATE']");
filterChainDefinitionManager.put("/admin/**","roles[Admin]");
filterChainDefinitionManager.put("/jobs/**","authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager);

shiroFilterFactoryBean.setSuccessUrl("/");
shiroFilterFactoryBean.setUnauthorizedUrl("/notAuthz");
return shiroFilterFactoryBean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ public ResponseEntity<UserDto> getUserInfo(UriComponentsBuilder uriComponentsBui

@GetMapping(value = "notAuthc")
public void notAuthc(UriComponentsBuilder uriComponentsBuilder){
throw new ResultException("Please Login", ErrorCode.NOTAUTHC);
throw new ResultException("Not Authc", ErrorCode.NOTAUTHC);
}

@GetMapping(value = "notAuthz")
public void notAuthz(UriComponentsBuilder uriComponentsBuilder){
throw new ResultException("Not Authz", ErrorCode.NOTAUTHZ);
UserDto loginUserDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("user");
throw new ResultException(
"UserName: " + loginUserDto.getName()
+ "\nPermissions: " + loginUserDto.getPermissions().toString()
+ "\nRoles: " + loginUserDto.getRoles().toString()
, ErrorCode.NOTAUTHC);
}

private UserDto convertToDto(User user){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import com.zhuxs.result.dto.TextDto;
import com.zhuxs.result.service.WordCountService;
import com.zhuxs.result.utils.ApplicationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.UriComponentsBuilder;
Expand All @@ -35,6 +37,7 @@ public class JobController {
@Autowired
private JobDao jobDao;

@RequiresPermissions("WORDCOUNT:CREATE")
@PostMapping(value = SUBPATH_WORDCOUNT)
public ResponseEntity<List<Count>> getCounts(@RequestBody TextDto words,
UriComponentsBuilder uriComponentsBuilder) throws ResultException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal
}
//赋予权限
for(PermissionDto permission:user.getPermissions()){
//System.out.println(permission.getName());
info.addStringPermission(permission.getName());
}
return info;
Expand Down
17 changes: 8 additions & 9 deletions UI/src/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,27 @@ const whiteList = ['/login','/'] //不重定向白名单
router.beforeEach((to,from,next) => {
NProgress.start(); //开启Progress
if(store.getters.status) {
if(to.path === '/login'){
next({path: '/'})
} else {
next()
}
next
} else {
if(whiteList.indexOf(to.path) !== -1){ //在登录白名单,直接进入
//alert("test")
next();
} else {
store.dispatch('GetUserInfo').then(res => {
//拉取user_info,并测试用户是否登录
const roles = res.data.roles
store.dispatch('GenerateRoutes',roles).then(() => {
console.log(store.getters.addRouters)
router.addRoutes(store.getters.addRouters) //动态添加可访问路由表
next({
to
})
console.log(router)
next()
//next({ ...to })
})
}).catch(e => {
if(e.response.data.errorCode === 40010){
if(e.response.data.errorCode.code === 40010){
next('/login') //否则全部定向到登录页
}
if(e.response.data.errorCode.code === 1)
next('/login') //否则全部定向到登录页
})
}
Expand Down
13 changes: 11 additions & 2 deletions UI/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,33 @@ export const constantRouterMap = [
{
path: "/admin",
component: _import('admin/index'),
//redirect: '/dashboard',
meta:{
role: ['Admin']
},
name: "admin"
},
{
path: "*",
redirect: "/",
name: "index"
component: _import('index/index'),
name: "Not Found"
}
]

export default new Router({
mode: 'history',
scrollBehavior: () => ({y:0}),
routes: constantRouterMap
})

export const asyncRouterMap = [
{
path: "/admin",
component: _import('admin/index'),
//redirect: '/dashboard',
meta:{
role: ['Admin']
},
name: "admin"
}
]
8 changes: 6 additions & 2 deletions UI/src/store/modules/Permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ const permission = {
SET_ROUTERS: (state, routers) => {
state.addRouters = routers
state.routers = constantRouterMap.concat(routers)
//alert(state.routers.length)
}
},
actions: {
GenerateRoutes({commit},data){
return new Promise(resolve => {
const roles = data
const roles = data.map(item => {
return item.name
})
let accessedRouters
if(roles.indexOf('admin') >= 0){

if(roles.indexOf('Admin') >= 0){
accessedRouters = asyncRouterMap
} else {
accessedRouters = filterAsyncRouter(asyncRouterMap,roles)
Expand Down
9 changes: 5 additions & 4 deletions UI/src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const user = {
const username = userInfo.username.trim()
return new Promise((resolve, reject) => {
login(username,userInfo.password).then(response => {
commit('SET_STATUS',true)
//commit('SET_STATUS',true)
resolve();
}).catch(error => {
reject(error)
Expand All @@ -48,11 +48,12 @@ const user = {
GetUserInfo({ commit } ){
return new Promise((resolve,reject) => {
getUserInfo().then(response => {
commit('SET_NAME',response.name)
commit('SET_USERNAME',response.username)
commit('SET_ROLES',response.roles)
commit('SET_NAME',response.data.name)
commit('SET_USERNAME',response.data.username)
commit('SET_ROLES',response.data.roles)
//commit('SET_')
commit('SET_STATUS',true)
//alert(state.name)
resolve(response)
}).catch(error => {
reject(error)
Expand Down
45 changes: 0 additions & 45 deletions UI/src/views/admin/userManagementPanel.vue

This file was deleted.

9 changes: 5 additions & 4 deletions UI/src/views/dashboard/wordCountPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</style>

<script>
import axios from 'axios'
import fetch from '../../utils/fetch'
export default{
data () {
return {
Expand Down Expand Up @@ -68,9 +68,10 @@
return
}
var postData = {"words" : this.words}
axios.post("http://localhost:8081/jobs/0",postData).then(response => {
this.data = response.data
this.loading = false
return new fetch({
path:'jobs/0',
method:"post",
postData
}).catch(e => {
this.$Notice.error({
title: 'ERROR',
Expand Down
23 changes: 20 additions & 3 deletions UI/src/views/login/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<div id="form">
<div v-if="status" id="form">
<signed></signed>
</div>
<div v-else id="guide">
<img id="img" src="../../../images/login.jpeg"/>
<signIn></signIn>
</div>
Expand All @@ -18,16 +21,30 @@
top: 0; left: 0; right: 0; bottom: 0;
overflow: auto;
}
#guide{
width: 500px;/* 宽度值,随便啦 */
height:500px;/* 高度值,随便啦 */
position: absolute;
margin: auto;
top: 0; left: 0; right: 0; bottom: 0;
overflow: auto;
}
</style>

<script>
import store from '../../store'
import signIn from "./signIn.vue"
import signed from "./signed.vue"
export default{
components: {
signIn
signIn,
signed
},
data () {
return {msg: 'hello'}
return {
msg: 'hello',
status: store.getters.status
}
}
}
</script>
25 changes: 25 additions & 0 deletions UI/src/views/login/signed.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div>
<Card>
<p slot="title">
<Icon type="person"></Icon>
{{ name }}
</p>

</Card>
</div>
</template>

<style>
</style>

<script>
import store from '../../store'
export default{
data () {
return {
name: store.getters.name
}
}
}
</script>

0 comments on commit c6359ac

Please sign in to comment.