-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt_patch.patch
663 lines (663 loc) · 25.7 KB
/
jwt_patch.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
Index: src/main/java/com/handson/basic/jwt/JwtAuthenticationController.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtAuthenticationController.java b/src/main/java/com/handson/basic/jwt/JwtAuthenticationController.java
new file mode 100644
--- /dev/null (date 1643146265417)
+++ b/src/main/java/com/handson/basic/jwt/JwtAuthenticationController.java (date 1643146265417)
@@ -0,0 +1,64 @@
+package com.handson.basic.jwt;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.authentication.DisabledException;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.userdetails.User;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+
+@RestController
+@CrossOrigin
+public class JwtAuthenticationController {
+
+ @Autowired
+ private AuthenticationManager authenticationManager;
+
+ @Autowired
+ private JwtTokenUtil jwtTokenUtil;
+
+ @Autowired
+ private JwtUserDetailsService userDetailsService;
+
+ @Autowired
+ private DBUserService userService;
+
+ @Autowired
+ private PasswordEncoder passwordEncoder;
+ @Autowired
+ AuthenticationManager am;
+
+ @RequestMapping(value = "/authenticate", method = RequestMethod.POST)
+ public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
+ authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
+ final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
+ final String token = jwtTokenUtil.generateToken(userDetails);
+ return ResponseEntity.ok(new JwtResponse(token));
+ }
+
+ @RequestMapping(value = "/user", method = RequestMethod.POST)
+ public ResponseEntity<?> createUser(@RequestBody JwtRequest userRequest) throws Exception {
+ String encodedPass = passwordEncoder.encode(userRequest.getPassword());
+ DBUser user = DBUser.UserBuilder.anUser().name(userRequest.getUsername())
+ .password(encodedPass).build();
+ userService.save(user);
+ UserDetails userDetails = new User(userRequest.getUsername(), encodedPass, new ArrayList<>());
+ return ResponseEntity.ok(new JwtResponse(jwtTokenUtil.generateToken(userDetails)));
+ }
+
+ private void authenticate(String username, String password) throws Exception {
+ try {
+ authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
+ } catch (DisabledException e) {
+ throw new Exception("USER_DISABLED", e);
+ } catch (BadCredentialsException e) {
+ throw new Exception("INVALID_CREDENTIALS", e);
+ }
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtUserDetailsService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtUserDetailsService.java b/src/main/java/com/handson/basic/jwt/JwtUserDetailsService.java
new file mode 100644
--- /dev/null (date 1643146265442)
+++ b/src/main/java/com/handson/basic/jwt/JwtUserDetailsService.java (date 1643146265442)
@@ -0,0 +1,29 @@
+package com.handson.basic.jwt;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.userdetails.User;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Optional;
+
+@Service
+public class JwtUserDetailsService implements UserDetailsService {
+
+ @Autowired
+ private DBUserService userService;
+
+ @Override
+ public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
+
+ Optional<DBUser> dbUser = userService.findUserName(userName);
+ if (dbUser.isPresent()) {
+ return new User(dbUser.get().getName(), dbUser.get().getPassword(), new ArrayList<>());
+ } else {
+ throw new UsernameNotFoundException("User not found : " + userName);
+ }
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtResponse.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtResponse.java b/src/main/java/com/handson/basic/jwt/JwtResponse.java
new file mode 100644
--- /dev/null (date 1643146265468)
+++ b/src/main/java/com/handson/basic/jwt/JwtResponse.java (date 1643146265468)
@@ -0,0 +1,16 @@
+package com.handson.basic.jwt;
+
+import java.io.Serializable;
+
+public class JwtResponse implements Serializable {
+ private static final long serialVersionUID = -8091879091924046844L;
+ private final String jwttoken;
+
+ public JwtResponse(String jwttoken) {
+ this.jwttoken = jwttoken;
+ }
+
+ public String getToken() {
+ return this.jwttoken;
+ }
+}
Index: src/main/java/com/handson/basic/jwt/UserService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/UserService.java b/src/main/java/com/handson/basic/jwt/UserService.java
new file mode 100644
--- /dev/null (date 1643146265438)
+++ b/src/main/java/com/handson/basic/jwt/UserService.java (date 1643146265438)
@@ -0,0 +1,11 @@
+package com.handson.basic.jwt;
+
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserService {
+
+
+ public void save(DBUser user) {
+ }
+}
Index: src/main/java/com/handson/basic/jwt/DBUserRepository.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/DBUserRepository.java b/src/main/java/com/handson/basic/jwt/DBUserRepository.java
new file mode 100644
--- /dev/null (date 1643146265422)
+++ b/src/main/java/com/handson/basic/jwt/DBUserRepository.java (date 1643146265422)
@@ -0,0 +1,9 @@
+package com.handson.basic.jwt;
+
+import org.springframework.data.repository.CrudRepository;
+
+import java.util.Optional;
+
+public interface DBUserRepository extends CrudRepository<DBUser,Long> {
+ Optional<DBUser> findByName(String name);
+}
Index: src/main/java/com/handson/basic/jwt/DBUser.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/DBUser.java b/src/main/java/com/handson/basic/jwt/DBUser.java
new file mode 100644
--- /dev/null (date 1643146265446)
+++ b/src/main/java/com/handson/basic/jwt/DBUser.java (date 1643146265446)
@@ -0,0 +1,113 @@
+package com.handson.basic.jwt;
+
+
+import com.google.common.base.MoreObjects;
+import org.springframework.data.domain.Persistable;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Entity
+@Table(name = "users")
+public class DBUser implements Serializable, Persistable<Long> {
+
+ private static final long serialVersionUID = -5554304839188669754L;
+
+ protected Long id;
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ @Access(AccessType.PROPERTY)
+ public Long getId() {
+ return id;
+ }
+
+ protected void setId(final Long id) {
+ this.id = id;
+ }
+
+ @Override
+ @Transient
+ public boolean isNew() {
+ return null == getId();
+ }
+
+
+ @Column(nullable = false, length = 60)
+ private String name;
+
+
+ @Column(nullable = false, length = 255)
+ private String password;
+
+ protected DBUser() {
+ }
+
+ @Transient
+ public static String hashPassword(String password) {
+ return new BCryptPasswordEncoder().encode(password);
+// return Hashing.sha256().hashString(password, Charset.defaultCharset()).toString();
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("id", getId())
+ .add("name", name)
+ .toString();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setPassword(String password) {
+ this.password = password; //DBUser.hashPassword(unencryptedPassword);
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+
+ public static final class UserBuilder {
+ protected Long id;
+ private String name;
+ private String password;//https://bcrypt-generator.com/ generate password user+email:javainuse,password:$2y$12$JfmXLQVmTZGpeYVgr6AVhejDGynQ739F4pJE1ZjyCPTvKIHTYb2fi
+
+ private UserBuilder() {
+ }
+
+ public static UserBuilder anUser() {
+ return new UserBuilder();
+ }
+
+ public UserBuilder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public UserBuilder password(String password) {
+ this.password = password;
+ return this;
+ }
+
+ public UserBuilder id(Long id) {
+ this.id = id;
+ return this;
+ }
+
+ public DBUser build() {
+ DBUser user = new DBUser();
+ user.setName(name);
+ user.setPassword(password);
+ user.setId(id);
+ return user;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/jwt/WebSecurityConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/WebSecurityConfig.java b/src/main/java/com/handson/basic/jwt/WebSecurityConfig.java
new file mode 100644
--- /dev/null (date 1643146265433)
+++ b/src/main/java/com/handson/basic/jwt/WebSecurityConfig.java (date 1643146265433)
@@ -0,0 +1,79 @@
+package com.handson.basic.jwt;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+
+@Configuration
+@EnableWebSecurity
+@EnableGlobalMethodSecurity(prePostEnabled = true)
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Autowired
+ private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
+
+ @Autowired
+ private UserDetailsService jwtUserDetailsService;
+
+ @Autowired
+ private JwtRequestFilter jwtRequestFilter;
+
+ @Autowired
+ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
+ // configure AuthenticationManager so that it knows from where to load
+ // user for matching credentials
+ // Use BCryptPasswordEncoder
+ auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
+ }
+
+ @Bean
+ public PasswordEncoder passwordEncoder() {
+ return new BCryptPasswordEncoder();
+ }
+
+ @Bean
+ @Override
+ public AuthenticationManager authenticationManagerBean() throws Exception {
+ return super.authenticationManagerBean();
+ }
+
+ @Override
+ protected void configure(HttpSecurity httpSecurity) throws Exception {
+
+ // We don't need CSRF for this example
+ // dont authenticate this particular request
+ httpSecurity.csrf().disable().authorizeRequests().antMatchers("/authenticate", "/user", "/actuator/**",
+ "/swagger-ui.html",
+ "/api/swagger-ui.html",
+ "/v2/api-docs",
+ "/configuration/ui",
+ "/swagger-resources/**",
+ "/configuration/security",
+ "/webjars/**").permitAll()
+ .antMatchers(HttpMethod.OPTIONS,"/**").permitAll().
+
+
+
+ // all other requests need to be authenticated
+ // make sure we use stateless session; session won't be used to
+ // store user's state.
+
+ anyRequest().authenticated().and().exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
+ .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
+
+ // Add a filter to validate the tokens with every request
+ httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtTokenUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtTokenUtil.java b/src/main/java/com/handson/basic/jwt/JwtTokenUtil.java
new file mode 100644
--- /dev/null (date 1643146649992)
+++ b/src/main/java/com/handson/basic/jwt/JwtTokenUtil.java (date 1643146649992)
@@ -0,0 +1,64 @@
+package com.handson.basic.jwt;
+
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.stereotype.Component;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+@Component
+public class JwtTokenUtil implements Serializable {
+ private static final long serialVersionUID = -2550185165626007488L;
+ public static final long JWT_TOKEN_VALIDITY = 5 * 60 * 60;
+
+// @Value("${jwt.secret}")
+ private String secret = "AppSecrettttt1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
+
+ public String getUsernameFromToken(String token) {
+ return getClaimFromToken(token, Claims::getSubject);
+ }
+ //retrieve expiration date from jwt token
+ public Date getExpirationDateFromToken(String token) {
+ return getClaimFromToken(token, Claims::getExpiration);
+ }
+
+ public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
+ final Claims claims = getAllClaimsFromToken(token);
+ return claimsResolver.apply(claims);
+ }
+ //for retrieving any information from token we will need the secret key
+ private Claims getAllClaimsFromToken(String token) {
+ return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
+ }
+
+ private Boolean isTokenExpired(String token) {
+ final Date expiration = getExpirationDateFromToken(token);
+ return expiration.before(new Date());
+ }
+
+ public String generateToken(UserDetails userDetails) {
+ Map<String, Object> claims = new HashMap<>();
+ return doGenerateToken(claims, userDetails.getUsername());
+ }
+ //while creating the token -
+//1. Define claims of the token, like Issuer, Expiration, Subject, and the ID
+//2. Sign the JWT using the HS512 algorithm and secret key.
+//3. According to JWS Compact Serialization(https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#section-3.1)
+// compaction of the JWT to a URL-safe string
+ private String doGenerateToken(Map<String, Object> claims, String subject) {
+ return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
+ .setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY * 1000))
+ .signWith(SignatureAlgorithm.HS512, secret).compact();
+ }
+ //validate token
+ public Boolean validateToken(String token, UserDetails userDetails) {
+ final String username = getUsernameFromToken(token);
+ return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtRequestFilter.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtRequestFilter.java b/src/main/java/com/handson/basic/jwt/JwtRequestFilter.java
new file mode 100644
--- /dev/null (date 1643146265462)
+++ b/src/main/java/com/handson/basic/jwt/JwtRequestFilter.java (date 1643146265462)
@@ -0,0 +1,65 @@
+package com.handson.basic.jwt;
+
+import io.jsonwebtoken.ExpiredJwtException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@Component
+public class JwtRequestFilter extends OncePerRequestFilter {
+
+ @Autowired
+ private JwtUserDetailsService jwtUserDetailsService;
+
+ @Autowired
+ private JwtTokenUtil jwtTokenUtil;
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
+ throws ServletException, IOException {
+ final String requestTokenHeader = request.getHeader("Authorization");
+ String username = null;
+ String jwtToken = null;
+// JWT Token is in the form "Bearer token". Remove Bearer word and get
+// only the Token
+ if (requestTokenHeader != null && requestTokenHeader.startsWith("Bearer ")) {
+ jwtToken = requestTokenHeader.substring(7);
+ try {
+ username = jwtTokenUtil.getUsernameFromToken(jwtToken);
+ } catch (IllegalArgumentException e) {
+ System.out.println("Unable to get JWT Token");
+ } catch (ExpiredJwtException e) {
+ System.out.println("JWT Token has expired");
+ }
+ } else if (!(request.getMethod().equals("OPTIONS")) && !request.getRequestURI().contains("actuator")){
+ logger.warn("JWT Token does not begin with Bearer String");
+ }
+// Once we get the token validate it.
+ if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
+ UserDetails userDetails = this.jwtUserDetailsService.loadUserByUsername(username);
+// if token is valid configure Spring Security to manually set
+// authentication
+ if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {
+ UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
+ userDetails, null, userDetails.getAuthorities());
+
+ usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
+// After setting the Authentication in the context, we specify
+// that the current user is authenticated. So it passes the
+// Spring Security Configurations successfully.
+ SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
+ }
+ }
+ chain.doFilter(request, response);
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtAuthenticationEntryPoint.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtAuthenticationEntryPoint.java b/src/main/java/com/handson/basic/jwt/JwtAuthenticationEntryPoint.java
new file mode 100644
--- /dev/null (date 1643146265473)
+++ b/src/main/java/com/handson/basic/jwt/JwtAuthenticationEntryPoint.java (date 1643146265473)
@@ -0,0 +1,33 @@
+package com.handson.basic.jwt;
+
+
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Serializable;
+
+@Component
+public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
+ private static final long serialVersionUID = -7858869558953243875L;
+
+ @Override
+ public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
+ response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
+ response.setHeader("Access-Control-Allow-Credentials", "true");
+ response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
+ response.setHeader("Access-Control-Max-Age", "3600");
+
+ response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Access-Control-Allow-Headers, Content-Type, Authorization, Origin, Accept, Referer, User-Agent," +
+ "Origin, sec-fetch-mode, sec-fetch-site, Accept, CloudFront-Viewer-Country, CloudFront-Is-Tablet-Viewer, CloudFront-Forwarded-Proto, "+
+ "X-Forwarded-Proto, User-Agent, Referer, CloudFront-Is-Mobile-Viewer, CloudFront-Is-SmartTV-Viewer, Host, Accept-Encoding, Pragma, "+
+ "X-Forwarded-Port, X-Amzn-Trace-Id, Via, Cache-Control, X-Forwarded-For, X-Amz-Cf-Id, Accept-Language, CloudFront-Is-Desktop-Viewer, "+
+ "sec-fetch-dest"
+ );
+ response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
+
+ }
+}
Index: src/main/java/com/handson/basic/jwt/JwtRequest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/JwtRequest.java b/src/main/java/com/handson/basic/jwt/JwtRequest.java
new file mode 100644
--- /dev/null (date 1643146265458)
+++ b/src/main/java/com/handson/basic/jwt/JwtRequest.java (date 1643146265458)
@@ -0,0 +1,34 @@
+package com.handson.basic.jwt;
+
+import java.io.Serializable;
+
+public class JwtRequest implements Serializable {
+ private static final long serialVersionUID = 5926468583005150707L;
+ private String username;
+ private String password;
+
+ //need default constructor for JSON Parsing
+ public JwtRequest() {
+ }
+
+ public JwtRequest(String username, String password) {
+ this.setUsername(username);
+ this.setPassword(password);
+ }
+
+ public String getUsername() {
+ return this.username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return this.password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+}
Index: src/main/java/com/handson/basic/jwt/DBUserService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/jwt/DBUserService.java b/src/main/java/com/handson/basic/jwt/DBUserService.java
new file mode 100644
--- /dev/null (date 1643146265428)
+++ b/src/main/java/com/handson/basic/jwt/DBUserService.java (date 1643146265428)
@@ -0,0 +1,26 @@
+package com.handson.basic.jwt;
+
+
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+@Service
+public class DBUserService {
+
+
+ private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DBUserService.class);
+
+ @Autowired
+ private DBUserRepository repository;
+
+ public Optional<DBUser> findUserName(String userName) {
+ return repository.findByName(userName);
+ }
+
+ public void save(DBUser user) {
+ repository.save(user);
+ }
+}