BAEL-1238 michael.good703@gmail.com (#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
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private String name;
|
||||
private String serviceRendered;
|
||||
private String address;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getServiceRendered() {
|
||||
return serviceRendered;
|
||||
}
|
||||
public void setServiceRendered(String serviceRendered) {
|
||||
this.serviceRendered = serviceRendered;
|
||||
}
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface CustomerDAO extends CrudRepository<Customer, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
|
||||
import org.keycloak.adapters.springsecurity.KeycloakSecurityComponents;
|
||||
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
|
||||
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
|
||||
import org.springframework.security.core.session.SessionRegistryImpl;
|
||||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
||||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
|
||||
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
|
||||
// Submits the KeycloakAuthenticationProvider to the AuthenticationManager
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
|
||||
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
|
||||
auth.authenticationProvider(keycloakAuthenticationProvider);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
|
||||
return new KeycloakSpringBootConfigResolver();
|
||||
}
|
||||
|
||||
// Specifies the session authentication strategy
|
||||
@Bean
|
||||
@Override
|
||||
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
|
||||
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
super.configure(http);
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/customers*")
|
||||
.hasRole("user")
|
||||
.anyRequest()
|
||||
.permitAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBoot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBoot.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@Controller
|
||||
public class WebController {
|
||||
|
||||
@Autowired
|
||||
private CustomerDAO customerDAO;
|
||||
|
||||
@GetMapping(path = "/")
|
||||
public String index() {
|
||||
return "external";
|
||||
}
|
||||
|
||||
@GetMapping(path = "/customers")
|
||||
public String customers(Principal principal, Model model) {
|
||||
addCustomers();
|
||||
Iterable<Customer> customers = customerDAO.findAll();
|
||||
model.addAttribute("customers", customers);
|
||||
model.addAttribute("username", principal.getName());
|
||||
return "customers";
|
||||
}
|
||||
|
||||
// add customers for demonstration
|
||||
public void addCustomers() {
|
||||
|
||||
Customer customer1 = new Customer();
|
||||
customer1.setAddress("1111 foo blvd");
|
||||
customer1.setName("Foo Industries");
|
||||
customer1.setServiceRendered("Important services");
|
||||
customerDAO.save(customer1);
|
||||
|
||||
Customer customer2 = new Customer();
|
||||
customer2.setAddress("2222 bar street");
|
||||
customer2.setName("Bar LLP");
|
||||
customer2.setServiceRendered("Important services");
|
||||
customerDAO.save(customer2);
|
||||
|
||||
Customer customer3 = new Customer();
|
||||
customer3.setAddress("33 main street");
|
||||
customer3.setName("Big LLC");
|
||||
customer3.setServiceRendered("Important services");
|
||||
customerDAO.save(customer3);
|
||||
}
|
||||
}
|
||||
@@ -49,3 +49,10 @@ contactInfoType=email
|
||||
|
||||
endpoints.beans.id=springbeans
|
||||
endpoints.beans.sensitive=false
|
||||
|
||||
#Keycloak Configuration
|
||||
keycloak.auth-server-url=http://localhost:8180/auth
|
||||
keycloak.realm=SpringBootKeycloak
|
||||
keycloak.resource=login-app
|
||||
keycloak.public-client=true
|
||||
keycloak.principal-attribute=preferred_username
|
||||
|
||||
33
spring-boot/src/main/resources/templates/customers.html
Normal file
33
spring-boot/src/main/resources/templates/customers.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:include="layout :: headerFragment">
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<h1>
|
||||
Hello, <span th:text="${username}">--name--</span>.
|
||||
</h1>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>Address</th>
|
||||
<th>Service Rendered</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="customer : ${customers}">
|
||||
<td th:text="${customer.id}">Text ...</td>
|
||||
<td th:text="${customer.name}">Text ...</td>
|
||||
<td th:text="${customer.address}">Text ...</td>
|
||||
<td th:text="${customer.serviceRendered}">Text...</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div id="pagefoot" th:include="layout :: footerFragment">Footer
|
||||
</div>
|
||||
</div>
|
||||
<!-- container -->
|
||||
</body>
|
||||
</html>
|
||||
31
spring-boot/src/main/resources/templates/external.html
Normal file
31
spring-boot/src/main/resources/templates/external.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:include="layout :: headerFragment">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron text-center">
|
||||
<h1>Customer Portal</h1>
|
||||
</div>
|
||||
<div>
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam
|
||||
erat lectus, vehicula feugiat ultricies at, tempus sed ante. Cras
|
||||
arcu erat, lobortis vitae quam et, mollis pharetra odio. Nullam sit
|
||||
amet congue ipsum. Nunc dapibus odio ut ligula venenatis porta non
|
||||
id dui. Duis nec tempor tellus. Suspendisse id blandit ligula, sit
|
||||
amet varius mauris. Nulla eu eros pharetra, tristique dui quis,
|
||||
vehicula libero. Aenean a neque sit amet tellus porttitor rutrum nec
|
||||
at leo.</p>
|
||||
|
||||
<h2>Existing Customers</h2>
|
||||
<div class="well">
|
||||
<b>Enter the intranet: </b><a th:href="@{/customers}">customers</a>
|
||||
</div>
|
||||
</div>
|
||||
<div id="pagefoot" th:include="layout :: footerFragment">Footer
|
||||
</div>
|
||||
</div>
|
||||
<!-- container -->
|
||||
|
||||
</body>
|
||||
</html>
|
||||
18
spring-boot/src/main/resources/templates/layout.html
Normal file
18
spring-boot/src/main/resources/templates/layout.html
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
<head th:fragment="headerFragment">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Customer Portal</title>
|
||||
<link
|
||||
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
|
||||
crossorigin="anonymous"></link>
|
||||
<link
|
||||
href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"
|
||||
rel="stylesheet"></link>
|
||||
</head>
|
||||
|
||||
<div id="pagefoot" th:fragment="footerFragment">
|
||||
<p>Document last modified 2017/10/23.</p>
|
||||
<p>Copyright: Lorem Ipsum</p>
|
||||
</div>
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.keycloak;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.keycloak.KeycloakPrincipal;
|
||||
import org.keycloak.KeycloakSecurityContext;
|
||||
import org.keycloak.adapters.springboot.client.KeycloakSecurityContextClientRequestInterceptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringBoot.class)
|
||||
public class KeycloakConfigurationTest {
|
||||
|
||||
@Spy
|
||||
private KeycloakSecurityContextClientRequestInterceptor factory;
|
||||
|
||||
private MockHttpServletRequest servletRequest;
|
||||
|
||||
@Mock
|
||||
public KeycloakSecurityContext keycloakSecurityContext;
|
||||
|
||||
@Mock
|
||||
private KeycloakPrincipal keycloakPrincipal;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
servletRequest = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(servletRequest));
|
||||
servletRequest.setUserPrincipal(keycloakPrincipal);
|
||||
when(keycloakPrincipal.getKeycloakSecurityContext()).thenReturn(keycloakSecurityContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKeycloakSecurityContext() throws Exception {
|
||||
assertNotNull(keycloakPrincipal.getKeycloakSecurityContext());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user