diff --git a/README.md b/README.md index 1e099d4..1d95520 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,19 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c > [Spring Boot + Angular 11 JWT Authentication](https://bezkoder.com/angular-11-spring-boot-jwt-auth/) +> [Spring Boot + Angular 12 JWT Authentication](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/) + > [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/) ## Fullstack CRUD App -> [Vue.js + Spring Boot + MySQL/PostgreSQL example](https://bezkoder.com/spring-boot-vue-js-crud-example/) +> [Vue.js + Spring Boot + H2 Embedded database example](https://www.bezkoder.com/spring-boot-vue-js-crud-example/) + +> [Vue.js + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-vue-js-mysql/) + +> [Vue.js + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-vue-js-postgresql/) + +> [Angular 8 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-spring-boot-crud/) > [Angular 8 + Spring Boot + MySQL example](https://bezkoder.com/angular-spring-boot-crud/) @@ -49,6 +57,18 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c > [Angular 11 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-11-spring-boot-postgresql/) +> [Angular 12 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-12-spring-boot-crud/) + +> [Angular 12 + Spring Boot + MySQL example](https://www.bezkoder.com/angular-12-spring-boot-mysql/) + +> [Angular 12 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-12-spring-boot-postgresql/) + +> [Angular 13 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-13-crud/) + +> [Angular 13 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-13-mysql/) + +> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/) + > [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/) > [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/) @@ -69,9 +89,10 @@ More Practice: > [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/) -> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://bezkoder.com/deploy-spring-boot-aws-eb/) +Deployment: +> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/) -> [Secure Spring Boot App with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/) +> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/) ## Dependency – If you want to use PostgreSQL: diff --git a/pom.xml b/pom.xml index 907ac6a..eed808f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.8.RELEASE + 2.6.1 com.bezkoder @@ -29,6 +29,11 @@ org.springframework.boot spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot diff --git a/src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java b/src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java index d13abd7..c723518 100644 --- a/src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java +++ b/src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java @@ -7,7 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; public class SpringBootSecurityJwtApplication { public static void main(String[] args) { - SpringApplication.run(SpringBootSecurityJwtApplication.class, args); + SpringApplication.run(SpringBootSecurityJwtApplication.class, args); } } diff --git a/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java b/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java index e598702..4287ba1 100644 --- a/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java +++ b/src/main/java/com/bezkoder/springjwt/controllers/AuthController.java @@ -36,94 +36,94 @@ import com.bezkoder.springjwt.security.services.UserDetailsImpl; @RestController @RequestMapping("/api/auth") public class AuthController { - @Autowired - AuthenticationManager authenticationManager; + @Autowired + AuthenticationManager authenticationManager; - @Autowired - UserRepository userRepository; + @Autowired + UserRepository userRepository; - @Autowired - RoleRepository roleRepository; + @Autowired + RoleRepository roleRepository; - @Autowired - PasswordEncoder encoder; + @Autowired + PasswordEncoder encoder; - @Autowired - JwtUtils jwtUtils; + @Autowired + JwtUtils jwtUtils; - @PostMapping("/signin") - public ResponseEntity authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { + @PostMapping("/signin") + public ResponseEntity authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { - Authentication authentication = authenticationManager.authenticate( - new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); + Authentication authentication = authenticationManager.authenticate( + new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); - SecurityContextHolder.getContext().setAuthentication(authentication); - String jwt = jwtUtils.generateJwtToken(authentication); - - UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal(); - List roles = userDetails.getAuthorities().stream() - .map(item -> item.getAuthority()) - .collect(Collectors.toList()); + SecurityContextHolder.getContext().setAuthentication(authentication); + String jwt = jwtUtils.generateJwtToken(authentication); + + UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal(); + List roles = userDetails.getAuthorities().stream() + .map(item -> item.getAuthority()) + .collect(Collectors.toList()); - return ResponseEntity.ok(new JwtResponse(jwt, - userDetails.getId(), - userDetails.getUsername(), - userDetails.getEmail(), - roles)); - } + return ResponseEntity.ok(new JwtResponse(jwt, + userDetails.getId(), + userDetails.getUsername(), + userDetails.getEmail(), + roles)); + } - @PostMapping("/signup") - public ResponseEntity registerUser(@Valid @RequestBody SignupRequest signUpRequest) { - if (userRepository.existsByUsername(signUpRequest.getUsername())) { - return ResponseEntity - .badRequest() - .body(new MessageResponse("Error: Username is already taken!")); - } + @PostMapping("/signup") + public ResponseEntity registerUser(@Valid @RequestBody SignupRequest signUpRequest) { + if (userRepository.existsByUsername(signUpRequest.getUsername())) { + return ResponseEntity + .badRequest() + .body(new MessageResponse("Error: Username is already taken!")); + } - if (userRepository.existsByEmail(signUpRequest.getEmail())) { - return ResponseEntity - .badRequest() - .body(new MessageResponse("Error: Email is already in use!")); - } + if (userRepository.existsByEmail(signUpRequest.getEmail())) { + return ResponseEntity + .badRequest() + .body(new MessageResponse("Error: Email is already in use!")); + } - // Create new user's account - User user = new User(signUpRequest.getUsername(), - signUpRequest.getEmail(), - encoder.encode(signUpRequest.getPassword())); + // Create new user's account + User user = new User(signUpRequest.getUsername(), + signUpRequest.getEmail(), + encoder.encode(signUpRequest.getPassword())); - Set strRoles = signUpRequest.getRole(); - Set roles = new HashSet<>(); + Set strRoles = signUpRequest.getRole(); + Set roles = new HashSet<>(); - if (strRoles == null) { - Role userRole = roleRepository.findByName(ERole.ROLE_USER) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(userRole); - } else { - strRoles.forEach(role -> { - switch (role) { - case "admin": - Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(adminRole); + if (strRoles == null) { + Role userRole = roleRepository.findByName(ERole.ROLE_USER) + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); + roles.add(userRole); + } else { + strRoles.forEach(role -> { + switch (role) { + case "admin": + Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); + roles.add(adminRole); - break; - case "mod": - Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(modRole); + break; + case "mod": + Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); + roles.add(modRole); - break; - default: - Role userRole = roleRepository.findByName(ERole.ROLE_USER) - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); - roles.add(userRole); - } - }); - } + break; + default: + Role userRole = roleRepository.findByName(ERole.ROLE_USER) + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); + roles.add(userRole); + } + }); + } - user.setRoles(roles); - userRepository.save(user); + user.setRoles(roles); + userRepository.save(user); - return ResponseEntity.ok(new MessageResponse("User registered successfully!")); - } + return ResponseEntity.ok(new MessageResponse("User registered successfully!")); + } } diff --git a/src/main/java/com/bezkoder/springjwt/controllers/TestController.java b/src/main/java/com/bezkoder/springjwt/controllers/TestController.java index 298a7ea..e03b5cb 100644 --- a/src/main/java/com/bezkoder/springjwt/controllers/TestController.java +++ b/src/main/java/com/bezkoder/springjwt/controllers/TestController.java @@ -10,26 +10,26 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/test") public class TestController { - @GetMapping("/all") - public String allAccess() { - return "Public Content."; - } - - @GetMapping("/user") - @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')") - public String userAccess() { - return "User Content."; - } + @GetMapping("/all") + public String allAccess() { + return "Public Content."; + } - @GetMapping("/mod") - @PreAuthorize("hasRole('MODERATOR')") - public String moderatorAccess() { - return "Moderator Board."; - } + @GetMapping("/user") + @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')") + public String userAccess() { + return "User Content."; + } - @GetMapping("/admin") - @PreAuthorize("hasRole('ADMIN')") - public String adminAccess() { - return "Admin Board."; - } + @GetMapping("/mod") + @PreAuthorize("hasRole('MODERATOR')") + public String moderatorAccess() { + return "Moderator Board."; + } + + @GetMapping("/admin") + @PreAuthorize("hasRole('ADMIN')") + public String adminAccess() { + return "Admin Board."; + } } diff --git a/src/main/java/com/bezkoder/springjwt/models/ERole.java b/src/main/java/com/bezkoder/springjwt/models/ERole.java index 9acf4a4..a113c68 100644 --- a/src/main/java/com/bezkoder/springjwt/models/ERole.java +++ b/src/main/java/com/bezkoder/springjwt/models/ERole.java @@ -1,7 +1,7 @@ package com.bezkoder.springjwt.models; public enum ERole { - ROLE_USER, - ROLE_MODERATOR, - ROLE_ADMIN + ROLE_USER, + ROLE_MODERATOR, + ROLE_ADMIN } diff --git a/src/main/java/com/bezkoder/springjwt/models/Role.java b/src/main/java/com/bezkoder/springjwt/models/Role.java index 44b82ad..abf816e 100644 --- a/src/main/java/com/bezkoder/springjwt/models/Role.java +++ b/src/main/java/com/bezkoder/springjwt/models/Role.java @@ -5,35 +5,35 @@ import javax.persistence.*; @Entity @Table(name = "roles") public class Role { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @Enumerated(EnumType.STRING) - @Column(length = 20) - private ERole name; + @Enumerated(EnumType.STRING) + @Column(length = 20) + private ERole name; - public Role() { + public Role() { - } + } - public Role(ERole name) { - this.name = name; - } + public Role(ERole name) { + this.name = name; + } - public Integer getId() { - return id; - } + public Integer getId() { + return id; + } - public void setId(Integer id) { - this.id = id; - } + public void setId(Integer id) { + this.id = id; + } - public ERole getName() { - return name; - } + public ERole getName() { + return name; + } - public void setName(ERole name) { - this.name = name; - } + public void setName(ERole name) { + this.name = name; + } } \ No newline at end of file diff --git a/src/main/java/com/bezkoder/springjwt/models/User.java b/src/main/java/com/bezkoder/springjwt/models/User.java index 6e3eaa2..9f03ab1 100644 --- a/src/main/java/com/bezkoder/springjwt/models/User.java +++ b/src/main/java/com/bezkoder/springjwt/models/User.java @@ -9,81 +9,81 @@ import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; @Entity -@Table( name = "users", - uniqueConstraints = { - @UniqueConstraint(columnNames = "username"), - @UniqueConstraint(columnNames = "email") - }) +@Table(name = "users", + uniqueConstraints = { + @UniqueConstraint(columnNames = "username"), + @UniqueConstraint(columnNames = "email") + }) public class User { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @NotBlank - @Size(max = 20) - private String username; + @NotBlank + @Size(max = 20) + private String username; - @NotBlank - @Size(max = 50) - @Email - private String email; + @NotBlank + @Size(max = 50) + @Email + private String email; - @NotBlank - @Size(max = 120) - private String password; + @NotBlank + @Size(max = 120) + private String password; - @ManyToMany(fetch = FetchType.LAZY) - @JoinTable( name = "user_roles", - joinColumns = @JoinColumn(name = "user_id"), - inverseJoinColumns = @JoinColumn(name = "role_id")) - private Set roles = new HashSet<>(); + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable( name = "user_roles", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "role_id")) + private Set roles = new HashSet<>(); - public User() { - } + public User() { + } - public User(String username, String email, String password) { - this.username = username; - this.email = email; - this.password = password; - } + public User(String username, String email, String password) { + this.username = username; + this.email = email; + this.password = password; + } - public Long getId() { - return id; - } + public Long getId() { + return id; + } - public void setId(Long id) { - this.id = id; - } + public void setId(Long id) { + this.id = id; + } - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) { + this.username = username; + } - public String getEmail() { - return email; - } + public String getEmail() { + return email; + } - public void setEmail(String email) { - this.email = email; - } + public void setEmail(String email) { + this.email = email; + } - public String getPassword() { - return password; - } + public String getPassword() { + return password; + } - public void setPassword(String password) { - this.password = password; - } + public void setPassword(String password) { + this.password = password; + } - public Set getRoles() { - return roles; - } + public Set getRoles() { + return roles; + } - public void setRoles(Set roles) { - this.roles = roles; - } + public void setRoles(Set roles) { + this.roles = roles; + } } diff --git a/src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java b/src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java index e0e7f9f..8b6bedf 100644 --- a/src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java +++ b/src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java @@ -4,7 +4,7 @@ import javax.validation.constraints.NotBlank; public class LoginRequest { @NotBlank - private String username; + private String username; @NotBlank private String password; diff --git a/src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java b/src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java index 515e0a5..899f199 100644 --- a/src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java +++ b/src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java @@ -3,52 +3,52 @@ package com.bezkoder.springjwt.payload.request; import java.util.Set; import javax.validation.constraints.*; - + public class SignupRequest { - @NotBlank - @Size(min = 3, max = 20) - private String username; - - @NotBlank - @Size(max = 50) - @Email - private String email; - - private Set role; - - @NotBlank - @Size(min = 6, max = 40) - private String password; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public Set getRole() { - return this.role; - } - - public void setRole(Set role) { - this.role = role; - } + @NotBlank + @Size(min = 3, max = 20) + private String username; + + @NotBlank + @Size(max = 50) + @Email + private String email; + + private Set role; + + @NotBlank + @Size(min = 6, max = 40) + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Set getRole() { + return this.role; + } + + public void setRole(Set role) { + this.role = role; + } } diff --git a/src/main/java/com/bezkoder/springjwt/payload/response/JwtResponse.java b/src/main/java/com/bezkoder/springjwt/payload/response/JwtResponse.java index 35ae5a4..ac387f0 100644 --- a/src/main/java/com/bezkoder/springjwt/payload/response/JwtResponse.java +++ b/src/main/java/com/bezkoder/springjwt/payload/response/JwtResponse.java @@ -3,62 +3,62 @@ package com.bezkoder.springjwt.payload.response; import java.util.List; public class JwtResponse { - private String token; - private String type = "Bearer"; - private Long id; - private String username; - private String email; - private List roles; + private String token; + private String type = "Bearer"; + private Long id; + private String username; + private String email; + private List roles; - public JwtResponse(String accessToken, Long id, String username, String email, List roles) { - this.token = accessToken; - this.id = id; - this.username = username; - this.email = email; - this.roles = roles; - } + public JwtResponse(String accessToken, Long id, String username, String email, List roles) { + this.token = accessToken; + this.id = id; + this.username = username; + this.email = email; + this.roles = roles; + } - public String getAccessToken() { - return token; - } + public String getAccessToken() { + return token; + } - public void setAccessToken(String accessToken) { - this.token = accessToken; - } + public void setAccessToken(String accessToken) { + this.token = accessToken; + } - public String getTokenType() { - return type; - } + public String getTokenType() { + return type; + } - public void setTokenType(String tokenType) { - this.type = tokenType; - } + public void setTokenType(String tokenType) { + this.type = tokenType; + } - public Long getId() { - return id; - } + public Long getId() { + return id; + } - public void setId(Long id) { - this.id = id; - } + public void setId(Long id) { + this.id = id; + } - public String getEmail() { - return email; - } + public String getEmail() { + return email; + } - public void setEmail(String email) { - this.email = email; - } + public void setEmail(String email) { + this.email = email; + } - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) { + this.username = username; + } - public List getRoles() { - return roles; - } + public List getRoles() { + return roles; + } } diff --git a/src/main/java/com/bezkoder/springjwt/payload/response/MessageResponse.java b/src/main/java/com/bezkoder/springjwt/payload/response/MessageResponse.java index 4a3a59f..068e179 100644 --- a/src/main/java/com/bezkoder/springjwt/payload/response/MessageResponse.java +++ b/src/main/java/com/bezkoder/springjwt/payload/response/MessageResponse.java @@ -1,17 +1,17 @@ package com.bezkoder.springjwt.payload.response; public class MessageResponse { - private String message; + private String message; - public MessageResponse(String message) { - this.message = message; - } + public MessageResponse(String message) { + this.message = message; + } - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } } diff --git a/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java b/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java index 06aa459..a1882d0 100644 --- a/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java +++ b/src/main/java/com/bezkoder/springjwt/repository/RoleRepository.java @@ -10,5 +10,5 @@ import com.bezkoder.springjwt.models.Role; @Repository public interface RoleRepository extends JpaRepository { - Optional findByName(ERole name); + Optional findByName(ERole name); } diff --git a/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java b/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java index 74f2df9..d48a98c 100644 --- a/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java +++ b/src/main/java/com/bezkoder/springjwt/repository/UserRepository.java @@ -9,9 +9,9 @@ import com.bezkoder.springjwt.models.User; @Repository public interface UserRepository extends JpaRepository { - Optional findByUsername(String username); + Optional findByUsername(String username); - Boolean existsByUsername(String username); + Boolean existsByUsername(String username); - Boolean existsByEmail(String email); + Boolean existsByEmail(String email); } diff --git a/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java b/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java index 405cf6b..b73080d 100644 --- a/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java +++ b/src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java @@ -21,46 +21,46 @@ import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity( - // securedEnabled = true, - // jsr250Enabled = true, - prePostEnabled = true) + // securedEnabled = true, + // jsr250Enabled = true, + prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { - @Autowired - UserDetailsServiceImpl userDetailsService; + @Autowired + UserDetailsServiceImpl userDetailsService; - @Autowired - private AuthEntryPointJwt unauthorizedHandler; + @Autowired + private AuthEntryPointJwt unauthorizedHandler; - @Bean - public AuthTokenFilter authenticationJwtTokenFilter() { - return new AuthTokenFilter(); - } + @Bean + public AuthTokenFilter authenticationJwtTokenFilter() { + return new AuthTokenFilter(); + } - @Override - public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { - authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); - } + @Override + public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { + authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); + } - @Bean - @Override - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); - } + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } - @Override - protected void configure(HttpSecurity http) throws Exception { - http.cors().and().csrf().disable() - .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() - .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() - .authorizeRequests().antMatchers("/api/auth/**").permitAll() - .antMatchers("/api/test/**").permitAll() - .anyRequest().authenticated(); + @Override + protected void configure(HttpSecurity http) throws Exception { + http.cors().and().csrf().disable() + .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() + .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() + .authorizeRequests().antMatchers("/api/auth/**").permitAll() + .antMatchers("/api/test/**").permitAll() + .anyRequest().authenticated(); - http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); - } + http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); + } } diff --git a/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java b/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java index b7e2647..903f6a8 100644 --- a/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java +++ b/src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java @@ -1,6 +1,8 @@ package com.bezkoder.springjwt.security.jwt; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -8,20 +10,34 @@ import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; + @Component public class AuthEntryPointJwt implements AuthenticationEntryPoint { - private static final Logger logger = LoggerFactory.getLogger(AuthEntryPointJwt.class); + private static final Logger logger = LoggerFactory.getLogger(AuthEntryPointJwt.class); - @Override - public void commence(HttpServletRequest request, HttpServletResponse response, - AuthenticationException authException) throws IOException, ServletException { - logger.error("Unauthorized error: {}", authException.getMessage()); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error: Unauthorized"); - } + @Override + public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) + throws IOException, ServletException { + logger.error("Unauthorized error: {}", authException.getMessage()); + + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + final Map body = new HashMap<>(); + body.put("status", HttpServletResponse.SC_UNAUTHORIZED); + body.put("error", "Unauthorized"); + body.put("message", authException.getMessage()); + body.put("path", request.getServletPath()); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(response.getOutputStream(), body); + } } diff --git a/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java b/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java index 4a906a8..2b3b6d3 100644 --- a/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java +++ b/src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java @@ -20,43 +20,46 @@ import org.springframework.web.filter.OncePerRequestFilter; import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl; public class AuthTokenFilter extends OncePerRequestFilter { - @Autowired - private JwtUtils jwtUtils; + @Autowired + private JwtUtils jwtUtils; - @Autowired - private UserDetailsServiceImpl userDetailsService; + @Autowired + private UserDetailsServiceImpl userDetailsService; - private static final Logger logger = LoggerFactory.getLogger(AuthTokenFilter.class); + private static final Logger logger = LoggerFactory.getLogger(AuthTokenFilter.class); - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) - throws ServletException, IOException { - try { - String jwt = parseJwt(request); - if (jwt != null && jwtUtils.validateJwtToken(jwt)) { - String username = jwtUtils.getUserNameFromJwtToken(jwt); + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { + try { + String jwt = parseJwt(request); + if (jwt != null && jwtUtils.validateJwtToken(jwt)) { + String username = jwtUtils.getUserNameFromJwtToken(jwt); - UserDetails userDetails = userDetailsService.loadUserByUsername(username); - UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( - userDetails, null, userDetails.getAuthorities()); - authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); + UserDetails userDetails = userDetailsService.loadUserByUsername(username); + UsernamePasswordAuthenticationToken authentication = + new UsernamePasswordAuthenticationToken( + userDetails, + null, + userDetails.getAuthorities()); + authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); - SecurityContextHolder.getContext().setAuthentication(authentication); - } - } catch (Exception e) { - logger.error("Cannot set user authentication: {}", e); - } + SecurityContextHolder.getContext().setAuthentication(authentication); + } + } catch (Exception e) { + logger.error("Cannot set user authentication: {}", e); + } - filterChain.doFilter(request, response); - } + filterChain.doFilter(request, response); + } - private String parseJwt(HttpServletRequest request) { - String headerAuth = request.getHeader("Authorization"); + private String parseJwt(HttpServletRequest request) { + String headerAuth = request.getHeader("Authorization"); - if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) { - return headerAuth.substring(7, headerAuth.length()); - } + if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) { + return headerAuth.substring(7, headerAuth.length()); + } - return null; - } + return null; + } } diff --git a/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java b/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java index 400b563..00c8157 100644 --- a/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java +++ b/src/main/java/com/bezkoder/springjwt/security/jwt/JwtUtils.java @@ -13,46 +13,46 @@ import io.jsonwebtoken.*; @Component public class JwtUtils { - private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); + private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); - @Value("${bezkoder.app.jwtSecret}") - private String jwtSecret; + @Value("${bezkoder.app.jwtSecret}") + private String jwtSecret; - @Value("${bezkoder.app.jwtExpirationMs}") - private int jwtExpirationMs; + @Value("${bezkoder.app.jwtExpirationMs}") + private int jwtExpirationMs; - public String generateJwtToken(Authentication authentication) { + public String generateJwtToken(Authentication authentication) { - UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal(); + UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal(); - return Jwts.builder() - .setSubject((userPrincipal.getUsername())) - .setIssuedAt(new Date()) - .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)) - .signWith(SignatureAlgorithm.HS512, jwtSecret) - .compact(); - } + return Jwts.builder() + .setSubject((userPrincipal.getUsername())) + .setIssuedAt(new Date()) + .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)) + .signWith(SignatureAlgorithm.HS512, jwtSecret) + .compact(); + } - public String getUserNameFromJwtToken(String token) { - return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject(); - } + public String getUserNameFromJwtToken(String token) { + return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject(); + } - public boolean validateJwtToken(String authToken) { - try { - Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); - return true; - } catch (SignatureException e) { - logger.error("Invalid JWT signature: {}", e.getMessage()); - } catch (MalformedJwtException e) { - logger.error("Invalid JWT token: {}", e.getMessage()); - } catch (ExpiredJwtException e) { - logger.error("JWT token is expired: {}", e.getMessage()); - } catch (UnsupportedJwtException e) { - logger.error("JWT token is unsupported: {}", e.getMessage()); - } catch (IllegalArgumentException e) { - logger.error("JWT claims string is empty: {}", e.getMessage()); - } + public boolean validateJwtToken(String authToken) { + try { + Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); + return true; + } catch (SignatureException e) { + logger.error("Invalid JWT signature: {}", e.getMessage()); + } catch (MalformedJwtException e) { + logger.error("Invalid JWT token: {}", e.getMessage()); + } catch (ExpiredJwtException e) { + logger.error("JWT token is expired: {}", e.getMessage()); + } catch (UnsupportedJwtException e) { + logger.error("JWT token is unsupported: {}", e.getMessage()); + } catch (IllegalArgumentException e) { + logger.error("JWT claims string is empty: {}", e.getMessage()); + } - return false; - } + return false; + } } diff --git a/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java b/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java index fafc5a9..6452f34 100644 --- a/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java +++ b/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsImpl.java @@ -13,91 +13,91 @@ import com.bezkoder.springjwt.models.User; import com.fasterxml.jackson.annotation.JsonIgnore; public class UserDetailsImpl implements UserDetails { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private Long id; + private Long id; - private String username; + private String username; - private String email; + private String email; - @JsonIgnore - private String password; + @JsonIgnore + private String password; - private Collection authorities; + private Collection authorities; - public UserDetailsImpl(Long id, String username, String email, String password, - Collection authorities) { - this.id = id; - this.username = username; - this.email = email; - this.password = password; - this.authorities = authorities; - } + public UserDetailsImpl(Long id, String username, String email, String password, + Collection authorities) { + this.id = id; + this.username = username; + this.email = email; + this.password = password; + this.authorities = authorities; + } - public static UserDetailsImpl build(User user) { - List authorities = user.getRoles().stream() - .map(role -> new SimpleGrantedAuthority(role.getName().name())) - .collect(Collectors.toList()); + public static UserDetailsImpl build(User user) { + List authorities = user.getRoles().stream() + .map(role -> new SimpleGrantedAuthority(role.getName().name())) + .collect(Collectors.toList()); - return new UserDetailsImpl( - user.getId(), - user.getUsername(), - user.getEmail(), - user.getPassword(), - authorities); - } + return new UserDetailsImpl( + user.getId(), + user.getUsername(), + user.getEmail(), + user.getPassword(), + authorities); + } - @Override - public Collection getAuthorities() { - return authorities; - } + @Override + public Collection getAuthorities() { + return authorities; + } - public Long getId() { - return id; - } + public Long getId() { + return id; + } - public String getEmail() { - return email; - } + public String getEmail() { + return email; + } - @Override - public String getPassword() { - return password; - } + @Override + public String getPassword() { + return password; + } - @Override - public String getUsername() { - return username; - } + @Override + public String getUsername() { + return username; + } - @Override - public boolean isAccountNonExpired() { - return true; - } + @Override + public boolean isAccountNonExpired() { + return true; + } - @Override - public boolean isAccountNonLocked() { - return true; - } + @Override + public boolean isAccountNonLocked() { + return true; + } - @Override - public boolean isCredentialsNonExpired() { - return true; - } + @Override + public boolean isCredentialsNonExpired() { + return true; + } - @Override - public boolean isEnabled() { - return true; - } + @Override + public boolean isEnabled() { + return true; + } - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - UserDetailsImpl user = (UserDetailsImpl) o; - return Objects.equals(id, user.id); - } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + UserDetailsImpl user = (UserDetailsImpl) o; + return Objects.equals(id, user.id); + } } diff --git a/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java b/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java index 9a3ed41..de65d7e 100644 --- a/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java +++ b/src/main/java/com/bezkoder/springjwt/security/services/UserDetailsServiceImpl.java @@ -12,16 +12,16 @@ import com.bezkoder.springjwt.repository.UserRepository; @Service public class UserDetailsServiceImpl implements UserDetailsService { - @Autowired - UserRepository userRepository; + @Autowired + UserRepository userRepository; - @Override - @Transactional - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - User user = userRepository.findByUsername(username) - .orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username)); + @Override + @Transactional + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username)); - return UserDetailsImpl.build(user); - } + return UserDetailsImpl.build(user); + } } diff --git a/src/test/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplicationTests.java b/src/test/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplicationTests.java index 60fe0c3..42ffdd0 100644 --- a/src/test/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplicationTests.java +++ b/src/test/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplicationTests.java @@ -1,11 +1,8 @@ package com.bezkoder.springjwt; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest public class SpringBootSecurityJwtApplicationTests {