22
33import com.techsoft.springsecurity.entity.AuthRequest;
44import com.techsoft.springsecurity.entity.UserInfo;
5+ import com.techsoft.springsecurity.logout.BlackList;
56import com.techsoft.springsecurity.service.JwtService;
67import com.techsoft.springsecurity.service.UserInfoService;
8+ import jakarta.servlet.http.HttpServletRequest;
79import org.springframework.beans.factory.annotation.Autowired;
810import org.springframework.security.access.prepost.PreAuthorize;
911import org.springframework.security.authentication.AuthenticationManager;
@@ -24,6 +26,9 @@ public class UserController {
2426 @Autowired
2527 private JwtService jwtService;
2628
29+ @Autowired
30+ private BlackList blackList;
31+
2732 @GetMapping("/welcome")
2833 public String welcome(){
2934 return "Welcome to Spring Security tutorials !!";
@@ -32,7 +37,6 @@ public String welcome(){
3237 @PostMapping("/addUser")
3338 public String addUser(@RequestBody UserInfo userInfo){
3439 return userInfoService.addUser(userInfo);
35-
3640 }
3741 @PostMapping("/login")
3842 public String addUser(@RequestBody AuthRequest authRequest){
@@ -43,8 +47,20 @@ public String addUser(@RequestBody AuthRequest authRequest){
4347 throw new UsernameNotFoundException("Invalid user request");
4448 }
4549 }
50+ @PostMapping("/logout")
51+ @PreAuthorize("hasAuthority('USER_ROLES') or hasAuthority('ADMIN_ROLES')")
52+ public String logoutUser(HttpServletRequest request){
53+ String authHeader = request.getHeader("Authorization");
54+ String token= null;
55+ if(authHeader !=null && authHeader.startsWith("Bearer")){
56+ token = authHeader.substring(7);
57+ }
58+ blackList.blacKListToken(token);
59+ return "You have successfully logged out !!";
60+ }
61+
4662 @GetMapping("/getUsers")
47- @PreAuthorize("hasAuthority('ADMIN_ROLES')")
63+ @PreAuthorize("hasAuthority('ADMIN_ROLES') or hasAuthority('USER_ROLES') ")
4864 public List<UserInfo> getAllUsers(){
4965 return userInfoService.getAllUser();
5066 }
0 commit comments