Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0ae20e

Browse files
动态更新权限配置
1 parent 814b2c9 commit b0ae20e

20 files changed

+352
-114
lines changed

‎pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@
7272
<artifactId>nekohtml</artifactId>
7373
<version>1.9.22</version>
7474
</dependency>
75+
<dependency>
76+
<groupId>com.github.theborakompanioni</groupId>
77+
<artifactId>thymeleaf-extras-shiro</artifactId>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.crazycake</groupId>
81+
<artifactId>shiro-redis</artifactId>
82+
<version>2.4.2.1-RELEASE</version>
83+
</dependency>
7584
</dependencies>
7685

7786
<build>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.study.config;
2+
3+
import org.springframework.cache.annotation.CachingConfigurerSupport;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* Created by yangqj on 2017年4月30日.
9+
*/
10+
@Configuration
11+
@EnableCaching
12+
public class RedisConfig extends CachingConfigurerSupport {
13+
}

‎src/main/java/com/study/config/ShiroConfig.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.study.config;
22

3+
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
34
import com.github.pagehelper.util.StringUtil;
45
import com.study.model.Resources;
56
import com.study.service.ResourcesService;
67
import com.study.shiro.MyShiroRealm;
78
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
89
import org.apache.shiro.mgt.SecurityManager;
9-
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
1010
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
1111
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
1212
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
1313
import org.springframework.beans.factory.annotation.Autowired;
1414
import org.springframework.context.annotation.Bean;
1515
import org.springframework.context.annotation.Configuration;
16-
import tk.mybatis.mapper.entity.Example;
1716

1817
import java.util.LinkedHashMap;
1918
import java.util.List;
@@ -27,6 +26,14 @@ public class ShiroConfig {
2726
@Autowired(required = false)
2827
private ResourcesService resourcesService;
2928

29+
/**
30+
* ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
31+
* @return
32+
*/
33+
@Bean
34+
public ShiroDialect shiroDialect() {
35+
return new ShiroDialect();
36+
}
3037
/**
3138
* ShiroFilterFactoryBean 处理拦截资源文件问题。
3239
* 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在
@@ -62,14 +69,12 @@ public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){
6269
filterChainDefinitionMap.put("/font-awesome/**","anon");
6370
//<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
6471
//<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
65-
filterChainDefinitionMap.put("/user", "authc");
6672
//自定义加载权限资源关系
6773
List<Resources> resourcesList = resourcesService.queryAll();
6874
for(Resources resources:resourcesList){
6975

70-
if (StringUtil.isNotEmpty(resources.getResurl())&& StringUtil.isNotEmpty(resources.getReskey())) {
71-
String permission = "perms[" + resources.getReskey()+ "]";
72-
System.out.println(resources.getResurl()+"---"+permission);
76+
if (StringUtil.isNotEmpty(resources.getResurl())) {
77+
String permission = "perms[" + resources.getResurl()+ "]";
7378
filterChainDefinitionMap.put(resources.getResurl(),permission);
7479
}
7580
}
@@ -127,5 +132,15 @@ public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(S
127132
return authorizationAttributeSourceAdvisor;
128133
}
129134

135+
/**
136+
* cacheManager 缓存 redis实现
137+
* 使用的是shiro-redis开源插件
138+
* @return
139+
*/
140+
public RedisCacheManager cacheManager() {
141+
RedisCacheManager redisCacheManager = new RedisCacheManager();
142+
redisCacheManager.setRedisManager(redisManager());
143+
return redisCacheManager;
144+
}
130145

131146
}

‎src/main/java/com/study/controller/HomeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String login(HttpServletRequest request, User user, Model model){
4747
return "login";
4848
}
4949
}
50-
@RequestMapping("/usersPage")
50+
@RequestMapping(value={"/usersPage",""})
5151
public String usersPage(){
5252
return "user/users";
5353
}

‎src/main/java/com/study/controller/ResourcesController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.pagehelper.PageInfo;
44
import com.study.model.Resources;
55
import com.study.service.ResourcesService;
6+
import com.study.shiro.ShiroService;
67
import org.apache.shiro.SecurityUtils;
78
import org.springframework.web.bind.annotation.RequestMapping;
89
import org.springframework.web.bind.annotation.RequestParam;
@@ -23,6 +24,8 @@ public class ResourcesController {
2324

2425
@Resource
2526
private ResourcesService resourcesService;
27+
@Resource
28+
private ShiroService shiroService;
2629

2730
@RequestMapping
2831
public Map<String,Object> getAll(Resources resources, String draw,
@@ -52,4 +55,30 @@ public List<Resources> loadMenu(){
5255
List<Resources> resourcesList = resourcesService.loadUserResources(map);
5356
return resourcesList;
5457
}
58+
59+
@RequestMapping(value = "/add")
60+
public String add(Resources resources){
61+
try{
62+
resourcesService.save(resources);
63+
//更新权限
64+
shiroService.updatePermission();
65+
return "success";
66+
}catch (Exception e){
67+
e.printStackTrace();
68+
return "fail";
69+
}
70+
}
71+
72+
@RequestMapping(value = "/delete")
73+
public String delete(Integer id){
74+
try{
75+
resourcesService.delete(id);
76+
//更新权限
77+
shiroService.updatePermission();
78+
return "success";
79+
}catch (Exception e){
80+
e.printStackTrace();
81+
return "fail";
82+
}
83+
}
5584
}

‎src/main/java/com/study/controller/RoleController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ public String add(Role role) {
7575
@RequestMapping(value = "/delete")
7676
public String delete(Integer id){
7777
try{
78-
roleService.delete(id);
78+
roleService.delRole(id);
7979
return "success";
8080
}catch (Exception e){
8181
e.printStackTrace();
8282
return "fail";
8383
}
8484
}
8585

86+
87+
8688
}

‎src/main/java/com/study/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public String add(User user) {
8181
@RequestMapping(value = "/delete")
8282
public String delete(Integer id){
8383
try{
84-
userService.delete(id);
84+
userService.delUser(id);
8585
return "success";
8686
}catch (Exception e){
8787
e.printStackTrace();

‎src/main/java/com/study/model/Resources.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ public class Resources {
1111
*/
1212
private String name;
1313

14-
/**
15-
* 资源key
16-
*/
17-
@Column(name = "resKey")
18-
private String reskey;
1914

2015
/**
2116
* 资源url
@@ -73,23 +68,6 @@ public void setName(String name) {
7368
this.name = name;
7469
}
7570

76-
/**
77-
* 获取资源key
78-
*
79-
* @return resKey - 资源key
80-
*/
81-
public String getReskey() {
82-
return reskey;
83-
}
84-
85-
/**
86-
* 设置资源key
87-
*
88-
* @param reskey 资源key
89-
*/
90-
public void setReskey(String reskey) {
91-
this.reskey = reskey;
92-
}
9371

9472
/**
9573
* 获取资源url
@@ -176,7 +154,6 @@ public String toString() {
176154
return "Resources{" +
177155
"id=" + id +
178156
", name='" + name + '\'' +
179-
", reskey='" + reskey + '\'' +
180157
", resurl='" + resurl + '\'' +
181158
", type=" + type +
182159
", parentid=" + parentid +

‎src/main/java/com/study/service/RoleService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ public interface RoleService extends IService<Role> {
1111

1212
PageInfo<Role> selectByPage(Role role, int start, int length);
1313

14-
14+
/**
15+
* 删除角色 同时删除角色资源表中的数据
16+
* @param roleid
17+
*/
18+
public void delRole(Integer roleid);
1519
}

‎src/main/java/com/study/service/UserService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface UserService extends IService<User>{
1212

1313
User selectByUsername(String username);
1414

15+
void delUser(Integer userid);
16+
1517
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /