refactor cloud security ex

This commit is contained in:
Loredana Crusoveanu 2018-01-17 22:57:20 +02:00
parent 8aadc36e24
commit 1cb252d052
27 changed files with 127 additions and 171 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>auth-server</artifactId> <artifactId>authserver</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>

View File

@ -1,15 +1,12 @@
package com.cloudsecurity.auth; package com.baeldung;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@SpringBootApplication @SpringBootApplication
public class AuthServer { public class AuthServer {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub SpringApplication.run(AuthServer.class, args);
SpringApplication.run(
AuthServer.class, args);
} }
} }

View File

@ -1,4 +1,4 @@
package com.cloudsecurity.auth.config; package com.baeldung.config;
import java.security.KeyPair; import java.security.KeyPair;

View File

@ -1,4 +1,4 @@
package com.cloudsecurity.auth.config; package com.baeldung.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@ -11,9 +11,6 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.R
@Configuration @Configuration
@EnableResourceServer @EnableResourceServer
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter { public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
@Override @Override
public void configure(HttpSecurity http) throws Exception { public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/user") http.antMatcher("/user")

View File

@ -1,10 +1,9 @@
package com.cloudsecurity.auth.config; package com.baeldung.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration @Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter { public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@ -12,4 +11,4 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) { public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("login").setViewName("login"); registry.addViewController("login").setViewName("login");
} }
} }

View File

@ -1,6 +1,5 @@
package com.cloudsecurity.auth.config; package com.baeldung.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
@ -9,7 +8,6 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
@Configuration @Configuration

View File

@ -1,4 +1,4 @@
package com.cloudsecurity.auth.controller; package com.baeldung.controller;
import java.security.Principal; import java.security.Principal;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;

View File

@ -2,18 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <groupId>com.baeldung</groupId>
<artifactId>springoath2</artifactId> <artifactId>oauth2client</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>springoath2</name> <name>oauth2client</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version> <version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>

View File

@ -1,20 +1,18 @@
package com.cloud.springwebsite; package com.baeldung;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.cloudsite.filters.pre.SimpleFilter; import com.baeldung.filters.SimpleFilter;
@SpringBootApplication @SpringBootApplication
public class CloudSite { public class CloudSite {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(CloudSite.class, args); SpringApplication.run(CloudSite.class, args);
} }
@Bean @Bean
public SimpleFilter simpleFilter() { public SimpleFilter simpleFilter() {

View File

@ -1,4 +1,4 @@
package com.cloud.springwebsite.config; package com.baeldung.config;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

View File

@ -1,11 +1,7 @@
package com.cloud.springwebsite.controller; package com.baeldung.controller;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestOperations; import org.springframework.web.client.RestOperations;
@ -13,27 +9,22 @@ import org.springframework.web.servlet.ModelAndView;
@RestController @RestController
public class CloudSiteController { public class CloudSiteController {
@Autowired @Autowired
private RestOperations restOperations; private RestOperations restOperations;
@GetMapping("/")
@Value("${person.url}")
private String personUrl;
@RequestMapping("/")
@ResponseBody @ResponseBody
public String helloFromBaeldung() { public String helloFromBaeldung() {
return "Hello From Baeldung!"; return "Hello From Baeldung!";
} }
@GetMapping("/person")
@RequestMapping("/person") public ModelAndView person() {
public ModelAndView person(){
ModelAndView mav = new ModelAndView("personinfo"); ModelAndView mav = new ModelAndView("personinfo");
mav.addObject("person",restOperations.getForObject(personUrl, String.class)); String personResourceUrl = "http://localhost:9000/personResource";
return mav; mav.addObject("person", restOperations.getForObject(personResourceUrl, String.class));
return mav;
} }
} }

View File

@ -0,0 +1,39 @@
package com.baeldung.filters;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SimpleFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
return null;
}
}

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Getting Personal Information</title>
</head>
<body>
<h1>Providing Person Information</h1>
<p>
Person's information: <span id="personInfo" th:text="${person}"></span>
</p>
</body>
</html>

View File

@ -3,18 +3,18 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.service</groupId> <groupId>com.baeldung</groupId>
<artifactId>personservice</artifactId> <artifactId>personresource</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>personservice</name> <name>personresource</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version> <version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>

View File

@ -1,12 +1,12 @@
package com.baeldung.service.personservice; package com.baeldung;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class PersonserviceApplication { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(PersonserviceApplication.class, args); SpringApplication.run(Application.class, args);
} }
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.service.personservice.config; package com.baeldung.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@ -21,5 +21,5 @@ public class ResourceConfigurer extends ResourceServerConfigurerAdapter {
http.httpBasic().disable(); http.httpBasic().disable();
http.authorizeRequests().anyRequest().authenticated(); http.authorizeRequests().anyRequest().authenticated();
} }
} }

View File

@ -0,0 +1,21 @@
package com.baeldung.controller;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.model.Person;
import com.google.gson.Gson;
@RestController
public class PersonInfoController {
@GetMapping("/personResource")
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
public @ResponseBody String personInfo() {
Gson gson = new Gson();
String person = gson.toJson(new Person("abir", "Dhaka", "Bangladesh", 29, "Male"));
return person;
}
}

View File

@ -1,51 +1,59 @@
package com.baeldung.service.model; package com.baeldung.model;
public class Person { public class Person {
private String name; private String name;
private String city; private String city;
private String country; private String country;
private Integer age; private Integer age;
private String sex; private String sex;
public Person(String name, String city, String country, Integer age, String sex){ public Person(String name, String city, String country, Integer age, String sex) {
this.name = name; this.name = name;
this.city = city; this.city = city;
this.country = country; this.country = country;
this.age = age; this.age = age;
this.sex = sex; this.sex = sex;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getCity() { public String getCity() {
return city; return city;
} }
public void setCity(String city) { public void setCity(String city) {
this.city = city; this.city = city;
} }
public String getCountry() { public String getCountry() {
return country; return country;
} }
public void setCountry(String country) { public void setCountry(String country) {
this.country = country; this.country = country;
} }
public Integer getAge() { public Integer getAge() {
return age; return age;
} }
public void setAge(Integer age) { public void setAge(Integer age) {
this.age = age; this.age = age;
} }
public String getSex() { public String getSex() {
return sex; return sex;
} }
public void setSex(String sex) { public void setSex(String sex) {
this.sex = sex; this.sex = sex;
} }
} }

View File

@ -1,7 +1,4 @@
# Make the application available at http://localhost:9000 # Make the application available at http://localhost:9000
#spring:
# session:
# store-type: redis
server: server:
port: 9000 port: 9000

View File

@ -9,8 +9,8 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest @SpringBootTest
public class PersonserviceApplicationTests { public class PersonserviceApplicationTests {
@Test @Test
public void contextLoads() { public void contextLoads() {
} }
} }

View File

@ -1,31 +0,0 @@
package com.baeldung.service.personservice.controller;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.service.model.Person;
import com.google.gson.Gson;
@RestController
public class PersonInfoController {
@RequestMapping(value = "/currenttime")
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
public String currentTime(){
return LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
}
@RequestMapping(value = "/person")
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
public @ResponseBody String personInfo(){
Gson gson = new Gson();
String person = gson.toJson(new Person("abir","Dhaka", "Bangladesh",29,"Male"));
return person;
}
}

View File

@ -1,39 +0,0 @@
package com.cloudsite.filters.pre;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SimpleFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
return null;
}
}

View File

@ -1,32 +0,0 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>My Website - Getting Personal Information</title>
<script th:inline="javascript">
/*<![CDATA[*/
function refreshTime() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("currentTime").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "/api/currenttime", true);
xhttp.send();
}
/*]]>*/
</script>
</head>
<body>
<h1>Providing Person Information</h1>
<p>
Person's information: <span id="personInfo" th:text="${person}"></span>
</p>
<p>
The current time is: <span id="currentTime"></span>
</p>
<button onclick="refreshTime();">GET Current Time</button>
</body>
</html>