refactor cloud security ex
This commit is contained in:
parent
8aadc36e24
commit
1cb252d052
|
@ -4,7 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>auth-server</artifactId>
|
||||
<artifactId>authserver</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
package com.cloudsecurity.auth;
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
SpringApplication.run(
|
||||
AuthServer.class, args);
|
||||
SpringApplication.run(AuthServer.class, args);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.cloudsecurity.auth.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.security.KeyPair;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.cloudsecurity.auth.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
|
@ -11,9 +11,6 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.R
|
|||
@Configuration
|
||||
@EnableResourceServer
|
||||
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/user")
|
|
@ -1,10 +1,9 @@
|
|||
package com.cloudsecurity.auth.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
|
||||
|
|
@ -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.Configuration;
|
||||
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.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
|
||||
@Configuration
|
|
@ -1,4 +1,4 @@
|
|||
package com.cloudsecurity.auth.controller;
|
||||
package com.baeldung.controller;
|
||||
|
||||
import java.security.Principal;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
@ -2,18 +2,18 @@
|
|||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>springoath2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>oauth2client</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>springoath2</name>
|
||||
<name>oauth2client</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.8.RELEASE</version>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
package com.cloud.springwebsite;
|
||||
package com.baeldung;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.cloudsite.filters.pre.SimpleFilter;
|
||||
import com.baeldung.filters.SimpleFilter;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
|
@ -15,7 +14,6 @@ public class CloudSite {
|
|||
SpringApplication.run(CloudSite.class, args);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public SimpleFilter simpleFilter() {
|
||||
return new SimpleFilter();
|
|
@ -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.cloud.netflix.zuul.EnableZuulProxy;
|
|
@ -1,11 +1,7 @@
|
|||
package com.cloud.springwebsite.controller;
|
||||
|
||||
import java.net.URI;
|
||||
package com.baeldung.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
|
@ -17,22 +13,17 @@ public class CloudSiteController {
|
|||
@Autowired
|
||||
private RestOperations restOperations;
|
||||
|
||||
|
||||
@Value("${person.url}")
|
||||
private String personUrl;
|
||||
|
||||
|
||||
@RequestMapping("/")
|
||||
@GetMapping("/")
|
||||
@ResponseBody
|
||||
public String helloFromBaeldung() {
|
||||
return "Hello From Baeldung!";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/person")
|
||||
public ModelAndView person(){
|
||||
@GetMapping("/person")
|
||||
public ModelAndView person() {
|
||||
ModelAndView mav = new ModelAndView("personinfo");
|
||||
mav.addObject("person",restOperations.getForObject(personUrl, String.class));
|
||||
String personResourceUrl = "http://localhost:9000/personResource";
|
||||
mav.addObject("person", restOperations.getForObject(personResourceUrl, String.class));
|
||||
return mav;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
|
@ -3,18 +3,18 @@
|
|||
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>
|
||||
|
||||
<groupId>com.baeldung.service</groupId>
|
||||
<artifactId>personservice</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>personresource</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>personservice</name>
|
||||
<name>personresource</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.8.RELEASE</version>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
package com.baeldung.service.personservice;
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PersonserviceApplication {
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PersonserviceApplication.class, args);
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.service.personservice.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.service.model;
|
||||
package com.baeldung.model;
|
||||
|
||||
public class Person {
|
||||
|
||||
|
@ -8,7 +8,7 @@ public class Person {
|
|||
private Integer age;
|
||||
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.city = city;
|
||||
this.country = country;
|
||||
|
@ -16,34 +16,42 @@ public class Person {
|
|||
this.sex = sex;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
}
|
|
@ -1,7 +1,4 @@
|
|||
# Make the application available at http://localhost:9000
|
||||
#spring:
|
||||
# session:
|
||||
# store-type: redis
|
||||
|
||||
server:
|
||||
port: 9000
|
|
@ -9,8 +9,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
@SpringBootTest
|
||||
public class PersonserviceApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
Loading…
Reference in New Issue