Skip to content

Commit 32a0a55

Browse files
fix 获取角色 token问题
1 parent 766f02f commit 32a0a55

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.example;
18+
19+
import io.geekidea.springbootplus.common.api.ApiResult;
20+
import io.swagger.annotations.Api;
21+
import io.swagger.annotations.ApiOperation;
22+
import lombok.extern.slf4j.Slf4j;
23+
import org.apache.shiro.authz.annotation.RequiresRoles;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RequestMethod;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
import javax.servlet.http.HttpServletResponse;
29+
import java.io.IOException;
30+
31+
/**
32+
* Shiro Example Controller
33+
*
34+
* @author geekidea
35+
* @date 2019-10-21
36+
**/
37+
@Slf4j
38+
@Api("Shiro Example")
39+
@RestController
40+
@RequestMapping("/shiroExample")
41+
public class ShiroExampleController {
42+
43+
@RequiresRoles("admin")
44+
@RequestMapping(value = "/hello", method = {RequestMethod.GET, RequestMethod.POST})
45+
@ApiOperation(value = "Shiro Example", notes = "Shiro Example", response = String.class)
46+
public ApiResult print(HttpServletResponse response) throws IOException {
47+
log.debug("Shiro Example...");
48+
return ApiResult.ok("Shiro Example");
49+
}
50+
51+
}

src/main/java/io/geekidea/springbootplus/shiro/jwt/JwtRealm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public boolean supports(AuthenticationToken token) {
6161
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
6262
log.debug("doGetAuthorizationInfo principalCollection...");
6363
// 设置角色/权限信息
64-
String token = principalCollection.toString();
64+
JwtToken jwtToken = (JwtToken) principalCollection.getPrimaryPrincipal();
6565
// 获取username
66-
String username = JwtUtil.getUsername(token);
66+
String username = jwtToken.getUsername();
6767
// 获取登陆用户角色权限信息
6868
LoginSysUserRedisVo loginSysUserRedisVo = loginRedisService.getLoginSysUserRedisVo(username);
6969
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

0 commit comments

Comments
 (0)