refactor cloud security ex
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung;
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.baeldung.filters.SimpleFilter;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class CloudSite {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudSite.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleFilter simpleFilter() {
|
||||
return new SimpleFilter();
|
||||
}
|
||||
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestOperations;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
||||
|
||||
@EnableZuulProxy
|
||||
@Configuration
|
||||
@EnableOAuth2Sso
|
||||
public class SiteSecurityConfigurer
|
||||
extends
|
||||
WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http)
|
||||
throws Exception {
|
||||
http.antMatcher("/**")
|
||||
.authorizeRequests()
|
||||
.antMatchers("/", "/webjars/**")
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.logout()
|
||||
.logoutSuccessUrl("/")
|
||||
.permitAll()
|
||||
.and()
|
||||
.csrf()
|
||||
.csrfTokenRepository(
|
||||
CookieCsrfTokenRepository
|
||||
.withHttpOnlyFalse());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OAuth2RestOperations restOperations(
|
||||
OAuth2ProtectedResourceDetails resource,
|
||||
OAuth2ClientContext context) {
|
||||
return new OAuth2RestTemplate(resource, context);
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@RestController
|
||||
public class CloudSiteController {
|
||||
|
||||
@Autowired
|
||||
private RestOperations restOperations;
|
||||
|
||||
@GetMapping("/")
|
||||
@ResponseBody
|
||||
public String helloFromBaeldung() {
|
||||
return "Hello From Baeldung!";
|
||||
}
|
||||
|
||||
@GetMapping("/person")
|
||||
public ModelAndView person() {
|
||||
ModelAndView mav = new ModelAndView("personinfo");
|
||||
String personResourceUrl = "http://localhost:9000/personResource";
|
||||
mav.addObject("person", restOperations.getForObject(personResourceUrl, String.class));
|
||||
return mav;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -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,37 @@
|
||||
# Make the application available at http://localhost:8080
|
||||
# These are default settings, but we add them for clarity.
|
||||
server:
|
||||
port: 8080
|
||||
contextPath: /
|
||||
|
||||
# Configure the Authorization Server and User Info Resource Server details
|
||||
security:
|
||||
oauth2:
|
||||
client:
|
||||
accessTokenUri: http://localhost:7070/authserver/oauth/token
|
||||
userAuthorizationUri: http://localhost:7070/authserver/oauth/authorize
|
||||
clientId: authserver
|
||||
clientSecret: passwordforauthserver
|
||||
resource:
|
||||
userInfoUri: http://localhost:7070/authserver/user
|
||||
|
||||
person:
|
||||
url: http://localhost:9000/person
|
||||
|
||||
# Proxies the calls to http://localhost:8080/api/* to our REST service at http://localhost:8081/*
|
||||
# and automatically includes our OAuth2 token in the request headers
|
||||
zuul:
|
||||
routes:
|
||||
resource:
|
||||
path: /api/**
|
||||
url: http://localhost:9000
|
||||
user:
|
||||
path: /user/**
|
||||
url: http://localhost:7070/authserver/user
|
||||
|
||||
# Make sure the OAuth2 token is only relayed when using the internal API,
|
||||
# do not pass any authentication to the external API
|
||||
proxy:
|
||||
auth:
|
||||
routes:
|
||||
api: oauth2
|
||||
+13
@@ -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>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.example.springoath2;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Springoath2ApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user