Skip to content

Commit 5778aae

Browse files
authored
Contract change (#2)
* feat: api naming convention updated * feat: added spotlessApply --------- Signed-off-by: Rajdeep Roy Chowdhury <[email protected]>
1 parent 6367485 commit 5778aae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+352
-299
lines changed

build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
id("io.spring.dependency-management") version "1.1.6"
44
kotlin("jvm") version "1.9.24" apply false // remove if not using Kotlin code
55
id("java")
6+
id("com.diffplug.spotless") version "8.1.0"
67
}
78

89
group = "com.razdeep"
@@ -19,6 +20,19 @@ repositories {
1920
mavenCentral()
2021
}
2122

23+
spotless {
24+
java {
25+
target("src/**/*.java")
26+
27+
palantirJavaFormat()
28+
removeUnusedImports()
29+
endWithNewline()
30+
trimTrailingWhitespace()
31+
32+
lineEndings = com.diffplug.spotless.LineEnding.UNIX
33+
}
34+
}
35+
2236
dependencies {
2337
implementation(libs.spring.boot.starter.web)
2438
implementation(libs.spring.boot.starter.security)

src/main/java/com/razdeep/konsignapi/KonsignApiApplication.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
77
import org.springframework.cache.annotation.EnableCaching;
88
import org.springframework.context.annotation.Bean;
9-
import org.springframework.context.annotation.ComponentScan;
109
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1110

12-
@SpringBootApplication(exclude= {UserDetailsServiceAutoConfiguration.class})
11+
@SpringBootApplication(exclude = {UserDetailsServiceAutoConfiguration.class})
1312
@EnableCaching
1413
public class KonsignApiApplication implements CommandLineRunner {
1514

@@ -23,7 +22,5 @@ public BCryptPasswordEncoder bCryptPasswordEncoder() {
2322
}
2423

2524
@Override
26-
public void run(String... args) {
27-
28-
}
25+
public void run(String... args) {}
2926
}

src/main/java/com/razdeep/konsignapi/config/GsonRedisSerializer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import com.google.gson.Gson;
44
import com.razdeep.konsignapi.model.CustomPageImpl;
5+
import java.io.UnsupportedEncodingException;
56
import org.springframework.data.redis.serializer.RedisSerializer;
67
import org.springframework.data.redis.serializer.SerializationException;
78

8-
import java.io.UnsupportedEncodingException;
9-
109
public class GsonRedisSerializer implements RedisSerializer {
1110
Gson gson = null;
1211

@@ -28,4 +27,4 @@ public Object deserialize(byte[] bytes) throws SerializationException {
2827
}
2928
return null;
3029
}
31-
}
30+
}

src/main/java/com/razdeep/konsignapi/config/KonsignConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.razdeep.konsignapi.config;
22

3-
import lombok.Data;
43
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.context.annotation.Configuration;
65

src/main/java/com/razdeep/konsignapi/config/RedisCacheConfig.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1010
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
1111
import com.razdeep.konsignapi.model.CustomPageImpl;
12+
import java.time.Duration;
1213
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.context.annotation.Bean;
1415
import org.springframework.context.annotation.Configuration;
@@ -21,9 +22,6 @@
2122
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
2223
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
2324
import org.springframework.data.redis.serializer.RedisSerializationContext;
24-
import org.springframework.data.redis.serializer.RedisSerializer;
25-
26-
import java.time.Duration;
2725

2826
@Configuration
2927
public class RedisCacheConfig {
@@ -53,9 +51,13 @@ public RedisCacheConfiguration cacheConfiguration() {
5351
return RedisCacheConfiguration.defaultCacheConfig()
5452
.entryTtl(Duration.ofMinutes(60))
5553
.disableCachingNullValues()
56-
// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GsonRedisSerializer()));
57-
// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
58-
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer()));
54+
//
55+
// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
56+
// GsonRedisSerializer()));
57+
//
58+
// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
59+
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(
60+
genericJackson2JsonRedisSerializer()));
5961
}
6062

6163
@Bean
@@ -71,9 +73,8 @@ public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
7173
mapper.registerSubtypes(PageImpl.class);
7274
mapper.registerSubtypes(CustomPageImpl.class);
7375

74-
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
75-
ObjectMapper.DefaultTyping.NON_FINAL,
76-
JsonTypeInfo.As.PROPERTY);
76+
mapper.activateDefaultTyping(
77+
mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
7778

7879
return new GenericJackson2JsonRedisSerializer(mapper);
7980
}
@@ -91,9 +92,8 @@ public Jackson2JsonRedisSerializer<Page> jackson2JsonRedisSerializer() {
9192
mapper.registerSubtypes(PageImpl.class);
9293
mapper.registerSubtypes(CustomPageImpl.class);
9394

94-
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
95-
ObjectMapper.DefaultTyping.NON_FINAL,
96-
JsonTypeInfo.As.PROPERTY);
95+
mapper.activateDefaultTyping(
96+
mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
9797

9898
Jackson2JsonRedisSerializer<Page> serializer = new Jackson2JsonRedisSerializer<>(Page.class);
9999
serializer.setObjectMapper(mapper);

src/main/java/com/razdeep/konsignapi/config/SecurityConfig.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.razdeep.konsignapi.config;
22

33
import com.razdeep.konsignapi.filter.JwtFilter;
4+
import java.util.Arrays;
5+
import java.util.List;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
68
import org.springframework.security.authentication.AuthenticationManager;
@@ -15,14 +17,10 @@
1517
import org.springframework.web.cors.CorsConfigurationSource;
1618
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
1719

18-
import java.util.Arrays;
19-
import java.util.List;
20-
2120
@Configuration
2221
@EnableWebSecurity
2322
public class SecurityConfig extends WebSecurityConfigurerAdapter {
2423

25-
2624
private final JwtFilter jwtFilter;
2725
private final UserDetailsService userDetailsService;
2826

@@ -38,12 +36,15 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
3836

3937
@Override
4038
protected void configure(HttpSecurity http) throws Exception {
41-
http.csrf().disable()
39+
http.csrf()
40+
.disable()
4241
.cors()
4342
.and()
4443
.authorizeRequests()
45-
.antMatchers("/authenticate", "/register", "/refreshtoken", "/actuator/**").permitAll()
46-
.antMatchers("/**").hasRole("USER")
44+
.antMatchers("/authenticate", "/register", "/refreshtoken", "/actuator/**")
45+
.permitAll()
46+
.antMatchers("/**")
47+
.hasRole("USER")
4748
.and()
4849
.httpBasic()
4950
.and()
@@ -69,4 +70,4 @@ CorsConfigurationSource corsConfigurationSource() {
6970
public AuthenticationManager authenticationManagerBean() throws Exception {
7071
return super.authenticationManagerBean();
7172
}
72-
}
73+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.razdeep.konsignapi.constant;
22

33
public class KonsignConstant {
4-
public final static String HEADER_REFRESH_TOKEN = "refresh-token";
5-
public final static String HEADER_CLAIMS = "claims";
4+
public static final String HEADER_REFRESH_TOKEN = "refresh-token";
5+
public static final String HEADER_CLAIMS = "claims";
6+
public static final String CONTROLLER_API_PREFIX = "/api/v1";
67
}

src/main/java/com/razdeep/konsignapi/controller/AuthenticationController.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.razdeep.konsignapi.controller;
22

33
import com.razdeep.konsignapi.config.KonsignConfig;
4+
import com.razdeep.konsignapi.constant.KonsignConstant;
45
import com.razdeep.konsignapi.exception.UsernameAlreadyExists;
56
import com.razdeep.konsignapi.model.AuthenticationRequest;
67
import com.razdeep.konsignapi.model.AuthenticationResponse;
@@ -10,6 +11,12 @@
1011
import com.razdeep.konsignapi.service.JwtUtilService;
1112
import com.razdeep.konsignapi.service.KonsignUserDetailsService;
1213
import io.jsonwebtoken.impl.DefaultClaims;
14+
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.Optional;
17+
import javax.servlet.http.Cookie;
18+
import javax.servlet.http.HttpServletRequest;
19+
import javax.servlet.http.HttpServletResponse;
1320
import lombok.val;
1421
import org.slf4j.Logger;
1522
import org.slf4j.LoggerFactory;
@@ -22,17 +29,8 @@
2229
import org.springframework.security.core.userdetails.UserDetails;
2330
import org.springframework.web.bind.annotation.*;
2431

25-
import javax.servlet.http.Cookie;
26-
import javax.servlet.http.HttpServletRequest;
27-
import javax.servlet.http.HttpServletResponse;
28-
import java.util.Arrays;
29-
import java.util.HashMap;
30-
import java.util.Optional;
31-
32-
import com.razdeep.konsignapi.constant.KonsignConstant;
33-
3432
@CrossOrigin
35-
@RestController
33+
@RestController(KonsignConstant.CONTROLLER_API_PREFIX)
3634
public class AuthenticationController {
3735

3836
private static final Logger LOG = LoggerFactory.getLogger(AuthenticationController.class);
@@ -43,34 +41,42 @@ public class AuthenticationController {
4341
private final AuthenticationService authenticationService;
4442

4543
@Autowired
46-
public AuthenticationController(AuthenticationManager authenticationManager, KonsignUserDetailsService konsignUserDetailsService, JwtUtilService jwtUtilService, AuthenticationService authenticationService) {
44+
public AuthenticationController(
45+
AuthenticationManager authenticationManager,
46+
KonsignUserDetailsService konsignUserDetailsService,
47+
JwtUtilService jwtUtilService,
48+
AuthenticationService authenticationService) {
4749
this.authenticationManager = authenticationManager;
4850
this.konsignUserDetailsService = konsignUserDetailsService;
4951
this.jwtUtilService = jwtUtilService;
5052
this.authenticationService = authenticationService;
5153
}
5254

5355
@PostMapping(value = "/authenticate")
54-
public ResponseEntity<?> authenticate(@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse response) {
56+
public ResponseEntity<?> authenticate(
57+
@RequestBody AuthenticationRequest authenticationRequest, HttpServletResponse response) {
5558
try {
56-
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(),
57-
authenticationRequest.getPassword()));
59+
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
60+
authenticationRequest.getUsername(), authenticationRequest.getPassword()));
5861
} catch (BadCredentialsException e) {
5962
return ResponseEntity.badRequest().body("Username or password mismatch");
6063
} catch (Exception e) {
6164
e.printStackTrace();
6265
}
6366

64-
final UserDetails konsignUserDetails = konsignUserDetailsService.loadUserByUsername(authenticationRequest.getUsername());
67+
final UserDetails konsignUserDetails =
68+
konsignUserDetailsService.loadUserByUsername(authenticationRequest.getUsername());
6569
final String accessToken = jwtUtilService.generateToken(konsignUserDetails);
6670
final AuthenticationResponse authenticationResponse = new AuthenticationResponse();
6771
authenticationResponse.setAccessToken(accessToken);
6872

69-
Cookie cookie = new Cookie(KonsignConstant.HEADER_REFRESH_TOKEN, jwtUtilService.doGenerateRefreshToken(new HashMap<>(), authenticationRequest.getUsername()));
73+
Cookie cookie = new Cookie(
74+
KonsignConstant.HEADER_REFRESH_TOKEN,
75+
jwtUtilService.doGenerateRefreshToken(new HashMap<>(), authenticationRequest.getUsername()));
7076

7177
cookie.setMaxAge(KonsignConfig.cookieMaxAge);
7278

73-
// cookie.setSecure(true);
79+
// cookie.setSecure(true);
7480
cookie.setHttpOnly(KonsignConfig.cookieHttpOnly);
7581
cookie.setPath(KonsignConfig.cookiePath);
7682

@@ -99,7 +105,9 @@ public ResponseEntity<?> refreshToken(HttpServletRequest request, HttpServletRes
99105

100106
try {
101107
if (jwtUtilService.validateToken(refreshToken, null)) {
102-
// jwtUtilService.validateToken(jwtUtilService.extractAccessTokenFromRequest(request), null);
108+
//
109+
// jwtUtilService.validateToken(jwtUtilService.extractAccessTokenFromRequest(request),
110+
// null);
103111
DefaultClaims claims = (DefaultClaims) request.getAttribute(KonsignConstant.HEADER_CLAIMS);
104112
val claimsMap = jwtUtilService.getMapFromIoJsonWebTokenClaims(claims);
105113
String jwtToken = jwtUtilService.doGenerateRefreshToken(claimsMap, (String) claimsMap.get("sub"));
@@ -126,4 +134,9 @@ public ResponseEntity<ResponseVerdict> register(@RequestBody UserRegistration us
126134
responseVerdict.setMessage("Successfully registered");
127135
return new ResponseEntity<>(responseVerdict, HttpStatus.OK);
128136
}
137+
138+
@GetMapping(value = "/")
139+
public ResponseEntity<String> welcome() {
140+
return new ResponseEntity<>("Welcome to konsign-api", HttpStatus.OK);
141+
}
129142
}

0 commit comments

Comments
 (0)