Skip to content

Commit 5688b5c

Browse files
committed
code improvement
1 parent 6e2b174 commit 5688b5c

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

src/main/java/com/github/pateluday07/graphqlspringbootdemo/config/security/CustomUserDetail.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.security.core.userdetails.UserDetails;
77

88
import java.util.Collection;
9+
import java.util.Collections;
910

1011
@NoArgsConstructor
1112
public class CustomUserDetail implements UserDetails {
@@ -20,7 +21,7 @@ public CustomUserDetail(Owner owner){
2021

2122
@Override
2223
public Collection<? extends GrantedAuthority> getAuthorities() {
23-
return null;
24+
return Collections.emptyList();
2425
}
2526

2627
@Override

src/main/java/com/github/pateluday07/graphqlspringbootdemo/config/security/JwtRequestFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected void doFilterInternal(HttpServletRequest request, @NotNull HttpServlet
4040

4141
UserDetails userDetails = this.userDetailService.loadUserByUsername(userName);
4242

43-
if (jwtUtil.validateToken(jwt, userDetails)) {
43+
if (Boolean.TRUE.equals(jwtUtil.validateToken(jwt, userDetails))) {
4444

4545
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
4646
userDetails, userDetails.getPassword(), userDetails.getAuthorities());

src/main/java/com/github/pateluday07/graphqlspringbootdemo/config/security/SecurityConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
3030

3131
@Override
3232
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
33-
auth.userDetailsService(userDetailService);
33+
auth
34+
.userDetailsService(userDetailService)
35+
.passwordEncoder(passwordEncoder());
3436
}
3537

3638
@Bean
@@ -54,7 +56,7 @@ protected void configure(HttpSecurity http) throws Exception {
5456
.permitAll()
5557
.antMatchers("/vendor/**", "/playground")
5658
.permitAll()
57-
.antMatchers("/*.ico","/altair")
59+
.antMatchers("/*.ico", "/altair")
5860
.permitAll()
5961
.antMatchers("/voyager")
6062
.permitAll()

src/main/java/com/github/pateluday07/graphqlspringbootdemo/dto/CarOwnerDto.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import lombok.Setter;
66
import lombok.ToString;
77

8-
import java.util.ArrayList;
9-
import java.util.List;
10-
118
@Getter
129
@Setter
1310
@ToString

src/main/java/com/github/pateluday07/graphqlspringbootdemo/exception/CustomException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import graphql.GraphQLError;
55
import graphql.language.SourceLocation;
66

7+
import java.util.Collections;
78
import java.util.HashMap;
89
import java.util.List;
910
import java.util.Map;
@@ -13,8 +14,8 @@
1314

1415
public class CustomException extends RuntimeException implements GraphQLError {
1516

16-
private Integer errorCode;
17-
private String errorMessage;
17+
private final Integer errorCode;
18+
private final String errorMessage;
1819

1920
public CustomException(Integer errorCode, String errorMessage) {
2021
super(errorMessage);
@@ -24,7 +25,7 @@ public CustomException(Integer errorCode, String errorMessage) {
2425

2526
@Override
2627
public List<SourceLocation> getLocations() {
27-
return null;
28+
return Collections.emptyList();
2829
}
2930

3031
@Override

src/main/java/com/github/pateluday07/graphqlspringbootdemo/service/OwnerServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void checkIfOwnerExistsById(Long id) {
100100
private void validateRegistrationDto(RegistrationDto registrationDto) {
101101
if (!EmailValidator.getInstance().isValid(registrationDto.getEmail()))
102102
throw new CustomException(Constants.BAD_REQUEST_ERROR_CODE, Constants.INVALID_EMAIL_ERROR_MSG);
103-
if (ownerRepository.existsByEmail(registrationDto.getEmail()))
103+
if (Boolean.TRUE.equals(ownerRepository.existsByEmail(registrationDto.getEmail())))
104104
throw new CustomException(Constants.BAD_REQUEST_ERROR_CODE, Constants.EMAIL_ALREADY_EXISTS_ERROR_MSG);
105105
if (!registrationDto.getPassword().equals(registrationDto.getConfirmPassword()))
106106
throw new CustomException(Constants.BAD_REQUEST_ERROR_CODE, Constants.CONFIRM_PASSWORD_DOES_NOT_MATCH_ERROR_MSG);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.pateluday07.graphqlspringbootdemo.service;
2+
3+
import com.github.pateluday07.graphqlspringbootdemo.config.security.JwtUtil;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.mockito.Mock;
6+
import org.mockito.junit.jupiter.MockitoExtension;
7+
import org.springframework.security.authentication.AuthenticationManager;
8+
9+
@ExtendWith(MockitoExtension.class)
10+
public class AuthenticationServiceImplUnitTest {
11+
12+
@Mock
13+
private JwtUtil jwtUtil;
14+
@Mock
15+
private AuthenticationManager authenticationManager;
16+
17+
18+
}

0 commit comments

Comments
 (0)