Skip to content

Commit b00a9e6

Browse files
author
eugenp
committed
formatting work
1 parent 1cba1b0 commit b00a9e6

36 files changed

+638
-626
lines changed

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/config/JWTCsrfTokenRepository.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
public class JWTCsrfTokenRepository implements CsrfTokenRepository {
1818

19-
private static final String DEFAULT_CSRF_TOKEN_ATTR_NAME = CSRFConfig.class.getName().concat(".CSRF_TOKEN");
19+
private static final String DEFAULT_CSRF_TOKEN_ATTR_NAME = CSRFConfig.class.getName()
20+
.concat(".CSRF_TOKEN");
2021

2122
private static final Logger log = LoggerFactory.getLogger(JWTCsrfTokenRepository.class);
2223
private byte[] secret;
@@ -27,10 +28,12 @@ public JWTCsrfTokenRepository(byte[] secret) {
2728

2829
@Override
2930
public CsrfToken generateToken(HttpServletRequest request) {
30-
String id = UUID.randomUUID().toString().replace("-", "");
31+
String id = UUID.randomUUID()
32+
.toString()
33+
.replace("-", "");
3134

3235
Date now = new Date();
33-
Date exp = new Date(System.currentTimeMillis() + (1000*30)); // 30 seconds
36+
Date exp = new Date(System.currentTimeMillis() + (1000 * 30)); // 30 seconds
3437

3538
String token = Jwts.builder()
3639
.setId(id)
@@ -50,8 +53,7 @@ public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletRe
5053
if (session != null) {
5154
session.removeAttribute(DEFAULT_CSRF_TOKEN_ATTR_NAME);
5255
}
53-
}
54-
else {
56+
} else {
5557
HttpSession session = request.getSession();
5658
session.setAttribute(DEFAULT_CSRF_TOKEN_ATTR_NAME, token);
5759
}

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/config/WebSecurityConfig.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,18 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
3030
SecretService secretService;
3131

3232
// ordered so we can use binary search below
33-
private String[] ignoreCsrfAntMatchers = {
34-
"/dynamic-builder-compress",
35-
"/dynamic-builder-general",
36-
"/dynamic-builder-specific",
37-
"/set-secrets"
38-
};
33+
private String[] ignoreCsrfAntMatchers = { "/dynamic-builder-compress", "/dynamic-builder-general", "/dynamic-builder-specific", "/set-secrets" };
3934

4035
@Override
4136
protected void configure(HttpSecurity http) throws Exception {
42-
http
43-
.addFilterAfter(new JwtCsrfValidatorFilter(), CsrfFilter.class)
37+
http.addFilterAfter(new JwtCsrfValidatorFilter(), CsrfFilter.class)
4438
.csrf()
45-
.csrfTokenRepository(jwtCsrfTokenRepository)
46-
.ignoringAntMatchers(ignoreCsrfAntMatchers)
47-
.and().authorizeRequests()
48-
.antMatchers("/**")
49-
.permitAll();
39+
.csrfTokenRepository(jwtCsrfTokenRepository)
40+
.ignoringAntMatchers(ignoreCsrfAntMatchers)
41+
.and()
42+
.authorizeRequests()
43+
.antMatchers("/**")
44+
.permitAll();
5045
}
5146

5247
private class JwtCsrfValidatorFilter extends OncePerRequestFilter {
@@ -58,13 +53,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
5853
CsrfToken token = (CsrfToken) request.getAttribute("_csrf");
5954

6055
if (
61-
// only care if it's a POST
62-
"POST".equals(request.getMethod()) &&
63-
// ignore if the request path is in our list
56+
// only care if it's a POST
57+
"POST".equals(request.getMethod()) &&
58+
// ignore if the request path is in our list
6459
Arrays.binarySearch(ignoreCsrfAntMatchers, request.getServletPath()) < 0 &&
6560
// make sure we have a token
66-
token != null
67-
) {
61+
token != null) {
6862
// CsrfFilter already made sure the token matched. Here, we'll make sure it's not expired
6963
try {
7064
Jwts.parser()

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/BaseController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
public class BaseController {
1212

1313
@ResponseStatus(HttpStatus.BAD_REQUEST)
14-
@ExceptionHandler({SignatureException.class, MalformedJwtException.class, JwtException.class})
14+
@ExceptionHandler({ SignatureException.class, MalformedJwtException.class, JwtException.class })
1515
public JwtResponse exception(Exception e) {
1616
JwtResponse response = new JwtResponse();
1717
response.setStatus(JwtResponse.Status.ERROR);
1818
response.setMessage(e.getMessage());
19-
response.setExceptionType(e.getClass().getName());
19+
response.setExceptionType(e.getClass()
20+
.getName());
2021

2122
return response;
2223
}

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,19 @@ public class DynamicJWTController extends BaseController {
2727

2828
@RequestMapping(value = "/dynamic-builder-general", method = POST)
2929
public JwtResponse dynamicBuilderGeneric(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
30-
String jws = Jwts.builder()
30+
String jws = Jwts.builder()
3131
.setClaims(claims)
32-
.signWith(
33-
SignatureAlgorithm.HS256,
34-
secretService.getHS256SecretBytes()
35-
)
32+
.signWith(SignatureAlgorithm.HS256, secretService.getHS256SecretBytes())
3633
.compact();
3734
return new JwtResponse(jws);
3835
}
3936

4037
@RequestMapping(value = "/dynamic-builder-compress", method = POST)
4138
public JwtResponse dynamicBuildercompress(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
42-
String jws = Jwts.builder()
39+
String jws = Jwts.builder()
4340
.setClaims(claims)
4441
.compressWith(CompressionCodecs.DEFLATE)
45-
.signWith(
46-
SignatureAlgorithm.HS256,
47-
secretService.getHS256SecretBytes()
48-
)
42+
.signWith(SignatureAlgorithm.HS256, secretService.getHS256SecretBytes())
4943
.compact();
5044
return new JwtResponse(jws);
5145
}
@@ -56,36 +50,36 @@ public JwtResponse dynamicBuilderSpecific(@RequestBody Map<String, Object> claim
5650

5751
claims.forEach((key, value) -> {
5852
switch (key) {
59-
case "iss":
60-
ensureType(key, value, String.class);
61-
builder.setIssuer((String) value);
62-
break;
63-
case "sub":
64-
ensureType(key, value, String.class);
65-
builder.setSubject((String) value);
66-
break;
67-
case "aud":
68-
ensureType(key, value, String.class);
69-
builder.setAudience((String) value);
70-
break;
71-
case "exp":
72-
ensureType(key, value, Long.class);
73-
builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
74-
break;
75-
case "nbf":
76-
ensureType(key, value, Long.class);
77-
builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
78-
break;
79-
case "iat":
80-
ensureType(key, value, Long.class);
81-
builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
82-
break;
83-
case "jti":
84-
ensureType(key, value, String.class);
85-
builder.setId((String) value);
86-
break;
87-
default:
88-
builder.claim(key, value);
53+
case "iss":
54+
ensureType(key, value, String.class);
55+
builder.setIssuer((String) value);
56+
break;
57+
case "sub":
58+
ensureType(key, value, String.class);
59+
builder.setSubject((String) value);
60+
break;
61+
case "aud":
62+
ensureType(key, value, String.class);
63+
builder.setAudience((String) value);
64+
break;
65+
case "exp":
66+
ensureType(key, value, Long.class);
67+
builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
68+
break;
69+
case "nbf":
70+
ensureType(key, value, Long.class);
71+
builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
72+
break;
73+
case "iat":
74+
ensureType(key, value, Long.class);
75+
builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
76+
break;
77+
case "jti":
78+
ensureType(key, value, String.class);
79+
builder.setId((String) value);
80+
break;
81+
default:
82+
builder.claim(key, value);
8983
}
9084
});
9185

@@ -95,13 +89,11 @@ public JwtResponse dynamicBuilderSpecific(@RequestBody Map<String, Object> claim
9589
}
9690

9791
private void ensureType(String registeredClaim, Object value, Class expectedType) {
98-
boolean isCorrectType =
99-
expectedType.isInstance(value) ||
100-
expectedType == Long.class && value instanceof Integer;
92+
boolean isCorrectType = expectedType.isInstance(value) || expectedType == Long.class && value instanceof Integer;
10193

10294
if (!isCorrectType) {
103-
String msg = "Expected type: " + expectedType.getCanonicalName() + " for registered claim: '" +
104-
registeredClaim + "', but got value: " + value + " of type: " + value.getClass().getCanonicalName();
95+
String msg = "Expected type: " + expectedType.getCanonicalName() + " for registered claim: '" + registeredClaim + "', but got value: " + value + " of type: " + value.getClass()
96+
.getCanonicalName();
10597
throw new JwtException(msg);
10698
}
10799
}

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/HomeController.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ public class HomeController {
1111
@RequestMapping("/")
1212
public String home(HttpServletRequest req) {
1313
String requestUrl = getUrl(req);
14-
return "Available commands (assumes httpie - https://github.com/jkbrzt/httpie):\n\n" +
15-
" http " + requestUrl + "/\n\tThis usage message\n\n" +
16-
" http " + requestUrl + "/static-builder\n\tbuild JWT from hardcoded claims\n\n" +
17-
" http POST " + requestUrl + "/dynamic-builder-general claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using general claims map)\n\n" +
18-
" http POST " + requestUrl + "/dynamic-builder-specific claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using specific claims methods)\n\n" +
19-
" http POST " + requestUrl + "/dynamic-builder-compress claim-1=value-1 ... [claim-n=value-n]\n\tbuild DEFLATE compressed JWT from passed in claims\n\n" +
20-
" http " + requestUrl + "/parser?jwt=<jwt>\n\tParse passed in JWT\n\n" +
21-
" http " + requestUrl + "/parser-enforce?jwt=<jwt>\n\tParse passed in JWT enforcing the 'iss' registered claim and the 'hasMotorcycle' custom claim\n\n" +
22-
" http " + requestUrl + "/get-secrets\n\tShow the signing keys currently in use.\n\n" +
23-
" http " + requestUrl + "/refresh-secrets\n\tGenerate new signing keys and show them.\n\n" +
24-
" http POST " + requestUrl + "/set-secrets HS256=base64-encoded-value HS384=base64-encoded-value HS512=base64-encoded-value\n\tExplicitly set secrets to use in the application.";
14+
return "Available commands (assumes httpie - https://github.com/jkbrzt/httpie):\n\n" + " http " + requestUrl + "/\n\tThis usage message\n\n" + " http " + requestUrl + "/static-builder\n\tbuild JWT from hardcoded claims\n\n" + " http POST "
15+
+ requestUrl + "/dynamic-builder-general claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using general claims map)\n\n" + " http POST " + requestUrl
16+
+ "/dynamic-builder-specific claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using specific claims methods)\n\n" + " http POST " + requestUrl
17+
+ "/dynamic-builder-compress claim-1=value-1 ... [claim-n=value-n]\n\tbuild DEFLATE compressed JWT from passed in claims\n\n" + " http " + requestUrl + "/parser?jwt=<jwt>\n\tParse passed in JWT\n\n" + " http " + requestUrl
18+
+ "/parser-enforce?jwt=<jwt>\n\tParse passed in JWT enforcing the 'iss' registered claim and the 'hasMotorcycle' custom claim\n\n" + " http " + requestUrl + "/get-secrets\n\tShow the signing keys currently in use.\n\n" + " http " + requestUrl
19+
+ "/refresh-secrets\n\tGenerate new signing keys and show them.\n\n" + " http POST " + requestUrl
20+
+ "/set-secrets HS256=base64-encoded-value HS384=base64-encoded-value HS512=base64-encoded-value\n\tExplicitly set secrets to use in the application.";
2521
}
2622

2723
private String getUrl(HttpServletRequest req) {
28-
return req.getScheme() + "://" +
29-
req.getServerName() +
30-
((req.getServerPort() == 80 || req.getServerPort() == 443) ? "" : ":" + req.getServerPort());
24+
return req.getScheme() + "://" + req.getServerName() + ((req.getServerPort() == 80 || req.getServerPort() == 443) ? "" : ":" + req.getServerPort());
3125
}
3226
}

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/StaticJWTController.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ public JwtResponse fixedBuilder() throws UnsupportedEncodingException {
3030
.setSubject("msilverman")
3131
.claim("name", "Micah Silverman")
3232
.claim("scope", "admins")
33-
.setIssuedAt(Date.from(Instant.ofEpochSecond(1466796822L))) // Fri Jun 24 2016 15:33:42 GMT-0400 (EDT)
33+
.setIssuedAt(Date.from(Instant.ofEpochSecond(1466796822L))) // Fri Jun 24 2016 15:33:42 GMT-0400 (EDT)
3434
.setExpiration(Date.from(Instant.ofEpochSecond(4622470422L))) // Sat Jun 24 2116 15:33:42 GMT-0400 (EDT)
35-
.signWith(
36-
SignatureAlgorithm.HS256,
37-
secretService.getHS256SecretBytes()
38-
)
35+
.signWith(SignatureAlgorithm.HS256, secretService.getHS256SecretBytes())
3936
.compact();
4037

4138
return new JwtResponse(jws);

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/model/JwtResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public enum Status {
1616
SUCCESS, ERROR
1717
}
1818

19-
public JwtResponse() {}
19+
public JwtResponse() {
20+
}
2021

2122
public JwtResponse(String jwt) {
2223
this.jwt = jwt;

jjwt/src/main/java/io/jsonwebtoken/jjwtfun/service/SecretService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public byte[] getHS512SecretBytes() {
6161
return TextCodec.BASE64.decode(secrets.get(SignatureAlgorithm.HS384.getValue()));
6262
}
6363

64-
6564
public Map<String, String> refreshSecrets() {
6665
SecretKey key = MacProvider.generateKey(SignatureAlgorithm.HS256);
6766
secrets.put(SignatureAlgorithm.HS256.getValue(), TextCodec.BASE64.encode(key.getEncoded()));

jpa-storedprocedure/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureIntegrationTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ public void createCarTest() {
5050
public void findCarsByYearNamedProcedure() {
5151
final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure");
5252
final StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
53-
storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
53+
storedProcedure.getResultList()
54+
.forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
5455
}
5556

5657
@Test
5758
public void findCarsByYearNoNamed() {
58-
final StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class).registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, 2015);
59-
storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
59+
final StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class)
60+
.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN)
61+
.setParameter(1, 2015);
62+
storedProcedure.getResultList()
63+
.forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
6064
}
6165

6266
@AfterClass

jsf/src/main/java/com/baeldung/springintegration/controllers/ELSampleBean.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ public class ELSampleBean {
2727
@PostConstruct
2828
public void init() {
2929
pageCounter = randomIntGen.nextInt();
30-
FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
31-
@Override
32-
public void contextCreated(ELContextEvent evt) {
33-
evt.getELContext().getImportHandler().importClass("com.baeldung.springintegration.controllers.ELSampleBean");
34-
}
35-
});
30+
FacesContext.getCurrentInstance()
31+
.getApplication()
32+
.addELContextListener(new ELContextListener() {
33+
@Override
34+
public void contextCreated(ELContextEvent evt) {
35+
evt.getELContext()
36+
.getImportHandler()
37+
.importClass("com.baeldung.springintegration.controllers.ELSampleBean");
38+
}
39+
});
3640
}
3741

3842
public void save() {
3943

4044
}
41-
45+
4246
public static String constantField() {
4347
return constantField;
4448
}
@@ -48,7 +52,8 @@ public void saveFirstName(String firstName) {
4852
}
4953

5054
public Long multiplyValue(LambdaExpression expr) {
51-
Long theResult = (Long) expr.invoke(FacesContext.getCurrentInstance().getELContext(), pageCounter);
55+
Long theResult = (Long) expr.invoke(FacesContext.getCurrentInstance()
56+
.getELContext(), pageCounter);
5257
return theResult;
5358
}
5459

0 commit comments

Comments
 (0)