Skip to content

Commit dc349c1

Browse files
michaelcgoodmaibin
authored andcommitted
BAEL-1238 michael.good703@gmail.com (eugenp#2879)
* michael.good703@gmail.com michael.good703@gmail.com * michael.good703@gmail.com michael.good703@gmail.com * michael.good703@gmail.com michael.good703@gmail.com * update
1 parent b121999 commit dc349c1

14 files changed

Lines changed: 476 additions & 1 deletion

File tree

‎spring-boot/.factorypath‎

Lines changed: 149 additions & 0 deletions
Large diffs are not rendered by default.

‎spring-boot/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.settings/
33
.classpath
44
.project
5+
/.apt_generated/

‎spring-boot/pom.xml‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
<groupId>org.springframework.boot</groupId>
2525
<artifactId>spring-boot-starter-web</artifactId>
2626
</dependency>
27-
27+
<dependency>
28+
<groupId>org.keycloak</groupId>
29+
<artifactId>keycloak-spring-boot-starter</artifactId>
30+
</dependency>
2831
<dependency>
2932
<groupId>org.springframework.boot</groupId>
3033
<artifactId>spring-boot-starter-data-jpa</artifactId>
@@ -170,6 +173,17 @@
170173
<artifactId>artemis-server</artifactId>
171174
</dependency>
172175
</dependencies>
176+
<dependencyManagement>
177+
<dependencies>
178+
<dependency>
179+
<groupId>org.keycloak.bom</groupId>
180+
<artifactId>keycloak-adapter-bom</artifactId>
181+
<version>3.3.0.CR2</version>
182+
<type>pom</type>
183+
<scope>import</scope>
184+
</dependency>
185+
</dependencies>
186+
</dependencyManagement>
173187

174188
<build>
175189
<finalName>spring-boot</finalName>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.keycloak;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Customer {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private long id;
13+
private String name;
14+
private String serviceRendered;
15+
private String address;
16+
17+
public long getId() {
18+
return id;
19+
}
20+
public void setId(long id) {
21+
this.id = id;
22+
}
23+
public String getName() {
24+
return name;
25+
}
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
public String getServiceRendered() {
30+
return serviceRendered;
31+
}
32+
public void setServiceRendered(String serviceRendered) {
33+
this.serviceRendered = serviceRendered;
34+
}
35+
public String getAddress() {
36+
return address;
37+
}
38+
public void setAddress(String address) {
39+
this.address = address;
40+
}
41+
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.keycloak;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface CustomerDAO extends CrudRepository<Customer, Long> {
6+
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.keycloak;
2+
3+
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
4+
import org.keycloak.adapters.springsecurity.KeycloakSecurityComponents;
5+
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
6+
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
14+
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
15+
import org.springframework.security.core.session.SessionRegistryImpl;
16+
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
17+
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
18+
19+
@Configuration
20+
@EnableWebSecurity
21+
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
22+
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
23+
// Submits the KeycloakAuthenticationProvider to the AuthenticationManager
24+
@Autowired
25+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
26+
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
27+
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
28+
auth.authenticationProvider(keycloakAuthenticationProvider);
29+
}
30+
31+
@Bean
32+
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
33+
return new KeycloakSpringBootConfigResolver();
34+
}
35+
36+
// Specifies the session authentication strategy
37+
@Bean
38+
@Override
39+
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
40+
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
41+
}
42+
43+
@Override
44+
protected void configure(HttpSecurity http) throws Exception {
45+
super.configure(http);
46+
http.authorizeRequests()
47+
.antMatchers("/customers*")
48+
.hasRole("user")
49+
.anyRequest()
50+
.permitAll();
51+
}
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.keycloak;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBoot {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBoot.class, args);
11+
}
12+
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.keycloak;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
7+
import java.security.Principal;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
11+
@Controller
12+
public class WebController {
13+
14+
@Autowired
15+
private CustomerDAO customerDAO;
16+
17+
@GetMapping(path = "/")
18+
public String index() {
19+
return "external";
20+
}
21+
22+
@GetMapping(path = "/customers")
23+
public String customers(Principal principal, Model model) {
24+
addCustomers();
25+
Iterable<Customer> customers = customerDAO.findAll();
26+
model.addAttribute("customers", customers);
27+
model.addAttribute("username", principal.getName());
28+
return "customers";
29+
}
30+
31+
// add customers for demonstration
32+
public void addCustomers() {
33+
34+
Customer customer1 = new Customer();
35+
customer1.setAddress("1111 foo blvd");
36+
customer1.setName("Foo Industries");
37+
customer1.setServiceRendered("Important services");
38+
customerDAO.save(customer1);
39+
40+
Customer customer2 = new Customer();
41+
customer2.setAddress("2222 bar street");
42+
customer2.setName("Bar LLP");
43+
customer2.setServiceRendered("Important services");
44+
customerDAO.save(customer2);
45+
46+
Customer customer3 = new Customer();
47+
customer3.setAddress("33 main street");
48+
customer3.setName("Big LLC");
49+
customer3.setServiceRendered("Important services");
50+
customerDAO.save(customer3);
51+
}
52+
}

‎spring-boot/src/main/resources/application.properties‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ contactInfoType=email
4949

5050
endpoints.beans.id=springbeans
5151
endpoints.beans.sensitive=false
52+
53+
#Keycloak Configuration
54+
keycloak.auth-server-url=http://localhost:8180/auth
55+
keycloak.realm=SpringBootKeycloak
56+
keycloak.resource=login-app
57+
keycloak.public-client=true
58+
keycloak.principal-attribute=preferred_username
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head th:include="layout :: headerFragment">
4+
</head>
5+
<body>
6+
<div id="container">
7+
<h1>
8+
Hello, <span th:text="${username}">--name--</span>.
9+
</h1>
10+
<table class="table table-striped">
11+
<thead>
12+
<tr>
13+
<th>ID</th>
14+
<th>Name</th>
15+
<th>Address</th>
16+
<th>Service Rendered</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
<tr th:each="customer : ${customers}">
21+
<td th:text="${customer.id}">Text ...</td>
22+
<td th:text="${customer.name}">Text ...</td>
23+
<td th:text="${customer.address}">Text ...</td>
24+
<td th:text="${customer.serviceRendered}">Text...</td>
25+
</tr>
26+
</tbody>
27+
</table>
28+
<div id="pagefoot" th:include="layout :: footerFragment">Footer
29+
</div>
30+
</div>
31+
<!-- container -->
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)