Skip to content

Commit 238681a

Browse files
committed
- pinned build to boot rc1
- added spring javaformat plugin - fixed broken tests
1 parent 8699840 commit 238681a

32 files changed

+862
-821
lines changed

authentication-provider/src/main/java/livelessons/custom/AuthenticationProviderApplication.java

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,84 +25,84 @@
2525
@SpringBootApplication
2626
public class AuthenticationProviderApplication {
2727

28-
public static void main(String args[]) {
29-
SpringApplication.run(AuthenticationProviderApplication.class, args);
28+
public static void main(String args[]) {
29+
SpringApplication.run(AuthenticationProviderApplication.class, args);
30+
}
31+
32+
@Configuration
33+
@EnableWebSecurity
34+
public static class BasicAuthorizationConfig extends WebSecurityConfigurerAdapter {
35+
36+
private final AtlassianCrowdAuthenticationProvider authenticationProvider;
37+
38+
public BasicAuthorizationConfig(
39+
AtlassianCrowdAuthenticationProvider authenticationProvider) {
40+
this.authenticationProvider = authenticationProvider;
3041
}
3142

32-
@Configuration
33-
@EnableWebSecurity
34-
public static class BasicAuthorizationConfig extends WebSecurityConfigurerAdapter {
35-
36-
private final AtlassianCrowdAuthenticationProvider authenticationProvider;
37-
38-
public BasicAuthorizationConfig(AtlassianCrowdAuthenticationProvider authenticationProvider) {
39-
this.authenticationProvider = authenticationProvider;
40-
}
41-
42-
@Override
43-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
44-
auth.authenticationProvider(this.authenticationProvider);
45-
}
46-
47-
@Override
48-
protected void configure(HttpSecurity http) throws Exception {
49-
http
50-
.csrf()
51-
.and()
52-
.httpBasic()
53-
.and()
54-
.authorizeRequests()
55-
.anyRequest().authenticated();
56-
}
43+
@Override
44+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
45+
auth.authenticationProvider(this.authenticationProvider);
5746
}
5847

48+
@Override
49+
protected void configure(HttpSecurity http) throws Exception {
50+
http.csrf().and().httpBasic().and().authorizeRequests().anyRequest()
51+
.authenticated();
52+
}
53+
54+
}
55+
5956
}
6057

6158
@RestController
6259
class GreetingsRestController {
6360

64-
@GetMapping("/greet")
65-
String greet(Principal p) {
66-
return "hello, " + p.getName() + "!";
67-
}
68-
}
61+
@GetMapping("/greet")
62+
String greet(Principal p) {
63+
return "hello, " + p.getName() + "!";
64+
}
6965

66+
}
7067

7168
@Component
7269
class AtlassianCrowdAuthenticationProvider implements AuthenticationProvider {
7370

74-
private final Log log = LogFactory.getLog(getClass());
71+
private final Log log = LogFactory.getLog(getClass());
7572

76-
// visible for testing.
77-
final String hardcodedUsername, hardcodedPassword;
73+
// visible for testing.
74+
final String hardcodedUsername, hardcodedPassword;
7875

79-
AtlassianCrowdAuthenticationProvider(
80-
@Value("${username:user}") String usr,
76+
AtlassianCrowdAuthenticationProvider(@Value("${username:user}") String usr,
8177
@Value("${password:pw}") String pw) {
82-
this.hardcodedUsername = usr;
83-
this.hardcodedPassword = pw;
84-
}
78+
this.hardcodedUsername = usr;
79+
this.hardcodedPassword = pw;
80+
}
8581

82+
@Override
83+
public Authentication authenticate(Authentication authentication)
84+
throws AuthenticationException {
8685

87-
@Override
88-
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
86+
String username = authentication.getName();
87+
String password = authentication.getCredentials().toString();
8988

90-
String username = authentication.getName();
91-
String password = authentication.getCredentials().toString();
89+
if (isValid(username, password)) {
90+
return new UsernamePasswordAuthenticationToken(username, password,
91+
AuthorityUtils.createAuthorityList("USER"));
92+
}
9293

93-
if (isValid(username, password)) {
94-
return new UsernamePasswordAuthenticationToken(username, password, AuthorityUtils.createAuthorityList("USER"));
95-
}
94+
throw new BadCredentialsException(
95+
"couldn't authenticate (" + authentication + ")");
96+
}
9697

97-
throw new BadCredentialsException("couldn't authenticate (" + authentication + ")");
98-
}
98+
private boolean isValid(String username, String password) {
99+
return (username.equalsIgnoreCase(this.hardcodedUsername)
100+
&& password.equalsIgnoreCase(this.hardcodedPassword));
101+
}
99102

100-
private boolean isValid(String username, String password) {
101-
return (username.equalsIgnoreCase(this.hardcodedUsername) && password.equalsIgnoreCase(this.hardcodedPassword));
102-
}
103+
@Override
104+
public boolean supports(Class<?> authentication) {
105+
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
106+
}
103107

104-
@Override
105-
public boolean supports(Class<?> authentication) {
106-
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
107-
}
108108
}

authentication-provider/src/main/java/livelessons/ldap/LdapAuthenticationApplication.java

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,45 @@
1616
@SpringBootApplication
1717
public class LdapAuthenticationApplication {
1818

19-
public static void main(String args[]) {
20-
SpringApplication.run(LdapAuthenticationApplication.class, args);
19+
public static void main(String args[]) {
20+
SpringApplication.run(LdapAuthenticationApplication.class, args);
21+
}
22+
23+
/**
24+
* @see <a href=
25+
* "https://www.quora.com/What-is-LDAP-and-how-does-LDAP-authentication-work">What is
26+
* LDAP</a>
27+
*/
28+
@Profile("ldap")
29+
@Configuration
30+
public static class LdapConfiguration extends WebSecurityConfigurerAdapter {
31+
32+
@Override
33+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
34+
35+
auth.ldapAuthentication().userDnPatterns("uid={0},ou=people")
36+
.groupSearchBase("ou=groups").contextSource()
37+
.url("ldap://localhost:8389/dc=springframework,dc=org").and()
38+
.passwordCompare().passwordEncoder(new LdapShaPasswordEncoder())
39+
.passwordAttribute("userPassword");
2140
}
2241

23-
/**
24-
* @see <a href="https://www.quora.com/What-is-LDAP-and-how-does-LDAP-authentication-work">What is LDAP</a>
25-
*/
26-
@Profile("ldap")
27-
@Configuration
28-
public static class LdapConfiguration extends WebSecurityConfigurerAdapter {
29-
30-
@Override
31-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
42+
@Override
43+
protected void configure(HttpSecurity http) throws Exception {
44+
http.csrf().and().httpBasic().and().authorizeRequests().anyRequest()
45+
.authenticated();
46+
}
3247

33-
auth
34-
.ldapAuthentication()
35-
.userDnPatterns("uid={0},ou=people")
36-
.groupSearchBase("ou=groups")
37-
.contextSource()
38-
.url("ldap://localhost:8389/dc=springframework,dc=org")
39-
.and()
40-
.passwordCompare()
41-
.passwordEncoder(new LdapShaPasswordEncoder())
42-
.passwordAttribute("userPassword");
43-
}
48+
}
4449

45-
@Override
46-
protected void configure(HttpSecurity http) throws Exception {
47-
http
48-
.csrf()
49-
.and()
50-
.httpBasic()
51-
.and()
52-
.authorizeRequests()
53-
.anyRequest().authenticated();
54-
}
55-
}
5650
}
5751

58-
5952
@RestController
6053
class GreetingsRestController {
6154

62-
@GetMapping("/greet")
63-
String greet(Principal p) {
64-
return "hello, " + p.getName() + "!";
65-
}
55+
@GetMapping("/greet")
56+
String greet(Principal p) {
57+
return "hello, " + p.getName() + "!";
58+
}
59+
6660
}

authentication-provider/src/test/java/livelessons/custom/AtlassianCrowdAuthenticationProviderTest.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,37 @@
99

1010
public class AtlassianCrowdAuthenticationProviderTest {
1111

12-
private final String username = "rob";
13-
private final String password = "b0r";
14-
private final AtlassianCrowdAuthenticationProvider provider = new AtlassianCrowdAuthenticationProvider(this.username, this.password);
15-
16-
@Test
17-
public void authenticateSuccess() {
18-
19-
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
20-
Authentication authenticate = this.provider.authenticate(token);
21-
Assert.assertTrue("the authentication should be valid", authenticate.isAuthenticated());
22-
}
23-
24-
@Test(expected = BadCredentialsException.class)
25-
public void authenticateFail() {
26-
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("WR0NG", "N0P3");
27-
this.provider.authenticate(token);
28-
Assert.fail("this should fail");
29-
}
30-
31-
@Test
32-
public void supports() {
33-
Assert.assertTrue(this.provider.supports(UsernamePasswordAuthenticationToken.class));
34-
Assert.assertTrue(this.provider.supports(JaasAuthenticationToken.class));
35-
Assert.assertFalse(this.provider.supports(RuntimeException.class));
36-
}
12+
private final String username = "rob";
13+
14+
private final String password = "b0r";
15+
16+
private final AtlassianCrowdAuthenticationProvider provider = new AtlassianCrowdAuthenticationProvider(
17+
this.username, this.password);
18+
19+
@Test
20+
public void authenticateSuccess() {
21+
22+
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
23+
username, password);
24+
Authentication authenticate = this.provider.authenticate(token);
25+
Assert.assertTrue("the authentication should be valid",
26+
authenticate.isAuthenticated());
27+
}
28+
29+
@Test(expected = BadCredentialsException.class)
30+
public void authenticateFail() {
31+
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
32+
"WR0NG", "N0P3");
33+
this.provider.authenticate(token);
34+
Assert.fail("this should fail");
35+
}
36+
37+
@Test
38+
public void supports() {
39+
Assert.assertTrue(
40+
this.provider.supports(UsernamePasswordAuthenticationToken.class));
41+
Assert.assertTrue(this.provider.supports(JaasAuthenticationToken.class));
42+
Assert.assertFalse(this.provider.supports(RuntimeException.class));
43+
}
44+
3745
}

0 commit comments

Comments
 (0)