SpringBoot项目,运行时忽略Shiro等功能权限和数据权限

编程入门 行业动态 更新时间:2024-10-25 16:25:49

SpringBoot项目,运行时忽略Shiro等功能<a href=https://www.elefans.com/category/jswz/34/1771295.html style=权限和数据权限"/>

SpringBoot项目,运行时忽略Shiro等功能权限和数据权限

 

几点感受: shiro等安全框架,非常复杂,组件太多,继承太复杂。老外写的框架都是这个鸟样。

只看本文的结论,很简单,但在找到答案之前,很复杂。

走了点弯路,shiro初始化比较早,据说在其它bean之前。不细说,大概记下。

 

配置属性:

# when only true,ignore shiro permission control
devMode: true

 

// 通用注释掉 注解的方式可以忽略权限,但非常麻烦

//  @Import(ShiroConfig.class)

public class Application {

}

 

初始化属性

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

@Component

@Slf4j

public class ConfigCommandLineRunner implements CommandLineRunner{

@Autowired

private Environment env;

 

public void initProperties() {

String devMode = env.getProperty("devMode");

if (StringUtils.equals(devMode, "true")) {

DevConfig.devMode = true;

log.info("ConfigCommandLineRunner run devMode is true");

}

}

 

@Override

public void run(String... arg0) throws Exception {

initProperties();

 

}

 

}

 

扩展Shiro的权限控制组件

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

/**

* 开发阶段,放过方法注解的拦截。提高开发效率.精益求精,快中求快。

*

*/

public class MyAuthorizationAttributeSourceAdvisor extends AuthorizationAttributeSourceAdvisor{

 

private static final long serialVersionUID = 1L;

 

@Override

public boolean matches(Method method, Class targetClass) {

if(DevConfig.devMode){

return false;

}

return super.matches(method, targetClass);

}

 

}

 

@Slf4j

public class MyFormAuthenticationFilter extends FormAuthenticationFilter {

 

@Override

protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {

log.info("MyFormAuthenticationFilter.isAccessAllowed()");

if(DevConfig.devMode){

return true;

}

return super.isAccessAllowed(request, response, mappedValue);

}

 

}

 

Shiro配置

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@Bean

public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {

AuthorizationAttributeSourceAdvisor aasa = new MyAuthorizationAttributeSourceAdvisor();

aasa.setSecurityManager(getDefaultWebSecurityManager());

return aasa;

}

 

 

 

@Bean(name = "shiroFilter")

public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager, CasFilter casFilter) {

ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

shiroFilterFactoryBean.setSecurityManager(securityManager);

 

Map<String, Filter> filters = new HashMap<>();

 

//自定义filter.

MyFormAuthenticationFilter formAuthenticationFilter = new MyFormAuthenticationFilter();

//重新定义formAuthenticationFilter

filters.put("authc", formAuthenticationFilter);

//交给spring shiroFilter管理

shiroFilterFactoryBean.setFilters(filters);

loadShiroFilterChain(shiroFilterFactoryBean);

return shiroFilterFactoryBean;

}

 

 

忽略数据权限,大概例子

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

/**

* 区域权限

*/

protected List<Integer> getAllAllowRegionId() {

Optional<Object> casJson = getSessionInfo("allowRegionId");

String allowRegionId = (String) casJson.orElse("");

if(DevConfig.devMode){

allowRegionId="0,4,5,6,7,9,12,14";

}

List<Integer> allowRegionIdList = JavaKit.splitAsIntList(allowRegionId, ",");

return allowRegionIdList;

}

 

 

 

/**

* 填充测试标记公司.没有测试权限时,设置isTest为0

*/

protected void fillIsTest(PermissionVo permissionVo) {

if(DevConfig.devMode){

log.info("DevMode true,ignore fillIsTest");

return;

}

boolean hasTestPermission=hasTestPermission();

if(!hasTestPermission){

permissionVo.setIsTest(0);

}

}

更多推荐

SpringBoot项目,运行时忽略Shiro等功能权限和数据权限

本文发布于:2024-02-11 19:25:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1682915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权限   等功能   项目   数据   SpringBoot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!