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 ee81751

Browse files
测试
1 parent cbf3f35 commit ee81751

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.study.config.shiro;
22

3+
import com.github.pagehelper.util.StringUtil;
4+
import com.study.model.Resources;
5+
import com.study.service.ResourcesService;
36
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
7+
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
48
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
59
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
10+
import org.springframework.beans.factory.annotation.Autowired;
611
import org.springframework.context.annotation.Bean;
712
import org.springframework.context.annotation.Configuration;
813
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
914
import org.apache.shiro.mgt.SecurityManager;
1015

16+
import javax.annotation.Resource;
1117
import java.util.LinkedHashMap;
18+
import java.util.List;
1219
import java.util.Map;
1320

1421
/**
1522
* Created by yangqj on 2017年4月23日.
1623
*/
1724
@Configuration
1825
public class ShiroConfig {
26+
@Autowired(required = false)
27+
private ResourcesService resourcesService;
1928

2029
/**
2130
* ShiroFilterFactoryBean 处理拦截资源文件问题。
@@ -35,22 +44,36 @@ public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){
3544

3645
// 必须设置 SecurityManager
3746
shiroFilterFactoryBean.setSecurityManager(securityManager);
47+
// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
48+
shiroFilterFactoryBean.setLoginUrl("/login");
49+
// 登录成功后要跳转的链接
50+
shiroFilterFactoryBean.setSuccessUrl("/user");
51+
//未授权界面;
52+
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
3853
//拦截器.
3954
Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
4055

4156
//配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
4257
filterChainDefinitionMap.put("/logout", "logout");
43-
filterChainDefinitionMap.put("/static/**","anon");
58+
filterChainDefinitionMap.put("/css/**","anon");
59+
filterChainDefinitionMap.put("/js/**","anon");
60+
filterChainDefinitionMap.put("/img/**","anon");
61+
filterChainDefinitionMap.put("/font-awesome/**","anon");
4462
//<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
4563
//<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
4664
filterChainDefinitionMap.put("/user", "authc");
65+
//自定义加载权限资源关系
66+
List<Resources> resourcesList = resourcesService.selectByExample(new Resources());
67+
for(Resources resources:resourcesList){
68+
69+
if (StringUtil.isNotEmpty(resources.getResurl())&& StringUtil.isNotEmpty(resources.getReskey())) {
70+
String permission = "perms[" + resources.getReskey()+ "]";
71+
System.out.println(resources.getResurl()+"---"+permission);
72+
filterChainDefinitionMap.put(resources.getResurl(),permission);
73+
}
74+
}
4775
filterChainDefinitionMap.put("/**", "authc");
48-
// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
49-
shiroFilterFactoryBean.setLoginUrl("/login");
50-
// 登录成功后要跳转的链接
51-
shiroFilterFactoryBean.setSuccessUrl("/user");
52-
//未授权界面;
53-
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
76+
5477

5578
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
5679
return shiroFilterFactoryBean;
@@ -103,4 +126,15 @@ public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(S
103126
return authorizationAttributeSourceAdvisor;
104127
}
105128

129+
/**
130+
* LifecycleBeanPostProcessor,这是个DestructionAwareBeanPostProcessor的子类,
131+
* 负责org.apache.shiro.util.Initializable类型bean的生命周期的,初始化和销毁。
132+
* 主要是AuthorizingRealm类的子类,以及EhCacheManager类。
133+
*/
134+
@Bean(name = "lifecycleBeanPostProcessor")
135+
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
136+
return new LifecycleBeanPostProcessor();
137+
}
138+
139+
106140
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.study.service;
2+
3+
import com.study.model.Resources;
4+
import org.springframework.stereotype.Service;
5+
6+
/**
7+
* Created by yangqj on 2017年4月25日.
8+
*/
9+
public interface ResourcesService extends IService<Resources> {
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.study.service.impl;
2+
3+
import com.study.model.Resources;
4+
import com.study.service.ResourcesService;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* Created by yangqj on 2017年4月25日.
9+
*/
10+
@Service
11+
public class ResourcesServiceImpl extends BaseService<Resources> implements ResourcesService {
12+
}

‎src/main/resources/templates/login.html‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
<html xmlns:th="http://www.thymeleaf.org">
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>``
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
66
<link rel="stylesheet" th:href="@{/css/matrix-login.css}" />
77
<link th:href="@{/font-awesome/css/font-awesome.css}" rel="stylesheet" />
88
<!--<link th:href='@{http://fonts.googleapis.com/css?family=Open+Sans:400,700,800}' rel='stylesheet' type='text/css'/>-->
9-
<title>登录页面</title>
109
</head>
1110
<body>
1211
<div id="loginbox"><!--/*@thymesVar id="msg" type="java.lang.String"*/-->

0 commit comments

Comments
(0)

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