package com.gichungasoftwares.ecom.filters;
import com.gichungasoftwares.ecom.utils.JwtUtil;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Component
@RequiredArgsConstructor
public class JwtRequestFilter extends OncePerRequestFilter {
//create objects for userDetailsService and JwtUtil
private final UserDetailsServiceImpl userDetailsService;
private final JwtUtil jwtUtil;
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// get the authheader of the request
String authHeader = request.getHeader("Authorization");
String token = null;
String username = null;
//add a validation to check if auth header is null
if(authHeader != null && authHeader.startsWith("Bearer ")){
// get the token from auth header
token = authHeader.substring(7);
username = jwtUtil.extractUsernameFromToken(token);
}
//check that the username is not null and that the security context holder is null
if(username != null && SecurityContextHolder.getContext().getAuthentication() == null){
// get the user details from the user details service
UserDetails userDetails = UserDetailsService.loadUserByUsername(username);
// validate token
if(jwtUtil.validateToken(token, userDetails)){
// create the user password authentication token
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(userDetails, null);
// set details of the request in our authentication token
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
// update the security context holder with latest auth token
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
filterChain.doFilter(request, response);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用javaspringboot框架和Angle开发的电子商务应用程序.zip
共17个文件
java:10个
properties:2个
xml:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 63 浏览量
2024-03-01
16:18:38
上传
评论
收藏 73KB ZIP 举报
温馨提示
使用javaspringboot框架和Angle开发的电子商务应用程序.zip
资源推荐
资源详情
资源评论
收起资源包目录
使用javaspringboot框架和Angle开发的电子商务应用程序.zip (17个子文件)
springEcomm-master
mvnw.cmd 7KB
pom.xml 2KB
src
test
java
com
gichungasoftwares
ecom
EcomApplicationTests.java 216B
main
resources
application.properties 247B
java
com
gichungasoftwares
ecom
repository
UserRepository.java 82B
services
jwt
UserDetailsServiceImpl.java 88B
controller
AuthController.java 78B
utils
JwtUtil.java 3KB
filters
JwtRequestFilter.java 3KB
enums
UserRole.java 92B
entity
User.java 502B
EcomApplication.java 315B
dto
AuthenticationRequest.java 78B
.mvn
wrapper
maven-wrapper.properties 233B
maven-wrapper.jar 61KB
mvnw 11KB
.gitignore 395B
共 17 条
- 1
资源评论
处处清欢
- 粉丝: 154
- 资源: 2504
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功