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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user