jwt怎么获取当前登录用户_认证后请求其他服务时,如何从JWT令牌中获取用户名?...

在使用JWT进行身份验证的Spring Boot应用中,通过Eureka服务注册和Zuul网关进行授权。当在Academics服务中需要从JWT令牌获取用户名时,发现直接使用Authentication对象获取为null。解决办法是配置Zuul,修改敏感头设置,确保Authorization头被转发到下游服务,从而使Academics服务能够正确解析JWT并获取用户名。
摘要由CSDN通过智能技术生成

I am developing a spring boot application having front end with angular6 and using Eureka for service registry and zuul for authentication gateway, both as separate service. I am using jwt for authentication.

When I hit the url localhost:8080/dis/academics/complaint/getMyComplaints with authorization header having value jwt token, then it goes to gateway service and then gateway forwards this to academics service after valid authentication at gateway.

Now I want to get username from token in academics service. How can I get it?

Controller in Gateway Service

@RestController

@RequestMapping("/dis")

public class AuthRestAPIs {

@PostMapping("/signin")

public ResponseEntity> authenticateUser(@Valid @RequestBody LoginForm loginRequest) {

Authentication authentication = authenticationManager.authenticate(

new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));

SecurityContextHolder.getContext().setAuthentication(authentication);

String jwt = jwtProvider.generateJwtToken(authentication);

//UserDetails userDetails = (UserDetails) authentication.getPrincipal();

UserPrinciple userPrincipal = (UserPrinciple) authentication.getPrincipal();

return ResponseEntity.ok(new JwtResponse(jwt, userPrincipal.getUsername(), userPrincipal.getUserType(), userPrincipal.getAuthorities()));

}

}

Controller in Academics Service

@RestController

@RequestMapping("/complaint")

public class ComplaintsController {

@RequestMapping(value = "/getMyComplaints", method = RequestMethod.GET)

public Object[] getMyComplaints(Authentication authentication)

{

String username = authentication.getName();

//System.out.println(username);

String user_type = "student";

List complaints = new ArrayList<>();

if(user_type.equals("student"))

{

Collections.addAll(complaints, cleanlinessComplaintRepository.findByCreatedBy(username));

Collections.addAll(complaints, leComplaintRepository.findByCreatedBy(username));

Collections.addAll(complaints, facultyComplaintRepository.findByCreatedBy(username));

Collections.addAll(complaints, otherComplaintRepository.findByCreatedBy(username));

}

return complaints.toArray();

}

}

I have tried using Authentication class but getting null value. How can I achieve this?

解决方案

Zuul by default does not forwards your Authorization header . You will have to explicitly configure in zuul to make it do this.

Your services must be on different server.

You have to change sensitve header settings in zuul properties (may be application.yml). By default it takes the following input.

zuul:

routes:

users:

path: /myusers/**

sensitiveHeaders: Cookie,Set-Cookie,Authorization

url: https://downstream

If you are very sure to forward your header in all services then only change this to

zuul:

routes:

users:

path: /myusers/**

sensitiveHeaders:

url: https://downstream

I hope this helps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值