Skip to content

Commit 5fce156

Browse files
Merge pull request #93 from geekidea/dev
🚔 优化Shiro方法验证RequiresPermissions
2 parents ee363ec + ef862fa commit 5fce156

26 files changed

+328
-161
lines changed

README-zh.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ public class SpringBootPlusGenerator {
194194
.setGeneratorMapper(true)
195195
.setGeneratorMapperXml(true);
196196

197+
// 是否生成Shiro RequiresPermissions注解
198+
codeGenerator.setRequiresPermissions(true);
199+
197200
// 是否覆盖已有文件
198201
codeGenerator.setFileOverride(true);
199202

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ public class SpringBootPlusGenerator {
196196
.setGeneratorMapper(true)
197197
.setGeneratorMapperXml(true);
198198

199+
// Generated RequiresPermissions Annotation
200+
codeGenerator.setRequiresPermissions(true);
201+
199202
// Overwrite existing file or not
200203
codeGenerator.setFileOverride(true);
201204

docs/db/mysql_spring_boot_plus.sql

Lines changed: 72 additions & 22 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@
284284
<!-- Shiro+JWT start -->
285285
<dependency>
286286
<groupId>org.apache.shiro</groupId>
287-
<artifactId>shiro-spring-boot-starter</artifactId>
287+
<artifactId>shiro-spring</artifactId>
288288
<version>${shiro.version}</version>
289289
</dependency>
290290

src/main/java/io/geekidea/springbootplus/common/api/ApiCode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public enum ApiCode {
5858

5959
AUTHENTICATION_EXCEPTION(5104, "登陆授权异常"),
6060

61+
UNAUTHENTICATED_EXCEPTION(5105, "没有访问权限"),
62+
63+
UNAUTHORIZED_EXCEPTION(5106, "没有访问权限"),
64+
6165

6266
;
6367

src/main/java/io/geekidea/springbootplus/common/exception/GlobalExceptionHandler.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.geekidea.springbootplus.system.exception.VerificationCodeException;
2323
import lombok.extern.slf4j.Slf4j;
2424
import org.apache.shiro.authc.AuthenticationException;
25+
import org.apache.shiro.authz.UnauthenticatedException;
26+
import org.apache.shiro.authz.UnauthorizedException;
2527
import org.springframework.http.HttpStatus;
2628
import org.springframework.http.converter.HttpMessageNotReadableException;
2729
import org.springframework.validation.BindingResult;
@@ -147,6 +149,35 @@ public ApiResult authenticationExceptionHandler(AuthenticationException exceptio
147149
.setMsg(exception.getMessage());
148150
}
149151

152+
153+
/**
154+
* 未认证异常处理
155+
*
156+
* @param exception
157+
* @return
158+
*/
159+
@ExceptionHandler(value = UnauthenticatedException.class)
160+
@ResponseStatus(HttpStatus.OK)
161+
public ApiResult unauthenticatedExceptionHandler(UnauthenticatedException exception) {
162+
log.error("unauthenticatedException:", exception);
163+
return ApiResult.fail(ApiCode.UNAUTHENTICATED_EXCEPTION);
164+
}
165+
166+
167+
/**
168+
* 未授权异常处理
169+
*
170+
* @param exception
171+
* @return
172+
*/
173+
@ExceptionHandler(value = UnauthorizedException.class)
174+
@ResponseStatus(HttpStatus.OK)
175+
public ApiResult unauthorizedExceptionHandler(UnauthorizedException exception) {
176+
log.error("unauthorizedException:", exception);
177+
return ApiResult.fail(ApiCode.UNAUTHORIZED_EXCEPTION);
178+
}
179+
180+
150181
/**
151182
* 默认的异常处理
152183
*

src/main/java/io/geekidea/springbootplus/core/aop/AbstractLogAop.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import io.geekidea.springbootplus.util.IpUtil;
2828
import lombok.Data;
2929
import lombok.extern.slf4j.Slf4j;
30+
import org.apache.commons.lang3.ArrayUtils;
31+
import org.apache.shiro.authz.annotation.*;
3032
import org.aspectj.lang.ProceedingJoinPoint;
3133
import org.aspectj.lang.Signature;
3234
import org.aspectj.lang.reflect.MethodSignature;
@@ -41,6 +43,7 @@
4143
import javax.servlet.http.HttpServletRequest;
4244
import javax.servlet.http.HttpServletResponse;
4345
import java.lang.annotation.Annotation;
46+
import java.lang.reflect.AnnotatedType;
4447
import java.lang.reflect.Method;
4548
import java.util.*;
4649

@@ -147,6 +150,13 @@ public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
147150
Annotation[][] annotations = method.getParameterAnnotations();
148151
boolean isRequestBody = isRequestBody(annotations);
149152
map.put("isRequestBody", isRequestBody);
153+
154+
AnnotatedType[] annotatedTypes = method.getAnnotatedParameterTypes();
155+
System.out.println(Arrays.toString(annotatedTypes));
156+
157+
// 获取Shiro注解值,并记录到map中
158+
handleShiroAnnotationValue(map, method);
159+
150160
// 设置请求参数
151161
Object requestParamJson = getRequestParamJsonString(joinPoint, request, requestMethod, contentType, isRequestBody);
152162
map.put("param", requestParamJson);
@@ -172,6 +182,48 @@ public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
172182
return result;
173183
}
174184

185+
/**
186+
* 获取Shiro注解值,并记录到map中
187+
*
188+
* @param map
189+
* @param method
190+
*/
191+
protected void handleShiroAnnotationValue(Map<String, Object> map, Method method) {
192+
RequiresRoles requiresRoles = method.getAnnotation(RequiresRoles.class);
193+
if (requiresRoles != null) {
194+
String[] requiresRolesValues = requiresRoles.value();
195+
if (ArrayUtils.isNotEmpty(requiresRolesValues)) {
196+
String requiresRolesString = Arrays.toString(requiresRolesValues);
197+
map.put("requiresRoles", requiresRolesString);
198+
}
199+
}
200+
201+
RequiresPermissions requiresPermissions = method.getAnnotation(RequiresPermissions.class);
202+
if (requiresPermissions != null) {
203+
String[] requiresPermissionsValues = requiresPermissions.value();
204+
if (ArrayUtils.isNotEmpty(requiresPermissionsValues)) {
205+
String requiresPermissionsString = Arrays.toString(requiresPermissionsValues);
206+
map.put("requiresPermissions", requiresPermissionsString);
207+
}
208+
}
209+
210+
RequiresAuthentication requiresAuthentication = method.getAnnotation(RequiresAuthentication.class);
211+
if (requiresAuthentication != null) {
212+
map.put("requiresAuthentication", true);
213+
}
214+
215+
RequiresUser requiresUser = method.getAnnotation(RequiresUser.class);
216+
if (requiresUser != null) {
217+
map.put("requiresUser", true);
218+
}
219+
220+
RequiresGuest requiresGuest = method.getAnnotation(RequiresGuest.class);
221+
if (requiresGuest != null) {
222+
map.put("requiresGuest", true);
223+
}
224+
225+
}
226+
175227
/**
176228
* 处理请求参数
177229
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.scheduled;
18+
19+
import lombok.extern.slf4j.Slf4j;
20+
import org.springframework.scheduling.annotation.Scheduled;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* Hello任务调度
25+
*
26+
* @author geekidea
27+
* @date 2019-10-29
28+
**/
29+
@Slf4j
30+
@Component
31+
public class HelloScheduled {
32+
33+
/**
34+
* 每小时执行一次
35+
*/
36+
@Scheduled(cron = "0 0 0/1 * * ? ")
37+
public void hello() {
38+
log.debug("Test Scheduled...");
39+
}
40+
41+
}

src/main/java/io/geekidea/springbootplus/shiro/config/ShiroConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package io.geekidea.springbootplus.shiro.config;
1818

1919
import com.alibaba.fastjson.JSON;
20-
import io.geekidea.springbootplus.filter.RequestPathFilter;
2120
import io.geekidea.springbootplus.core.properties.SpringBootPlusFilterProperties;
21+
import io.geekidea.springbootplus.filter.RequestPathFilter;
2222
import io.geekidea.springbootplus.shiro.cache.LoginRedisService;
2323
import io.geekidea.springbootplus.shiro.exception.ShiroConfigException;
2424
import io.geekidea.springbootplus.shiro.jwt.JwtCredentialsMatcher;
@@ -126,7 +126,7 @@ public DefaultSubjectDAO subjectDAO() {
126126
* @return
127127
*/
128128
@Bean
129-
public DefaultWebSecurityManager securityManager(LoginRedisService loginRedisService) {
129+
public SecurityManager securityManager(LoginRedisService loginRedisService) {
130130
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
131131
securityManager.setRealm(jwtRealm(loginRedisService));
132132
securityManager.setSubjectDAO(subjectDAO());

src/main/java/io/geekidea/springbootplus/shiro/controller/LoginController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class LoginController {
4949
private LoginService loginService;
5050

5151
@PostMapping("/login")
52-
@ApiOperation(value = "登陆", notes = "系统用户登陆", response = ApiResult.class)
52+
@ApiOperation(value = "登陆", notes = "系统用户登陆", response = LoginSysUserTokenVo.class)
5353
public ApiResult login(@Valid @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {
5454
LoginSysUserTokenVo loginSysUserTokenVo = loginService.login(loginParam);
5555
// 设置token响应头

0 commit comments

Comments
 (0)