From 64a91eff4c14c1f06d39e2f3281fb8e076b7a0b8 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 8 Feb 2015 14:08:54 +0200 Subject: [PATCH] spring oath reddit --- spring-oauth/.classpath | 29 ++ spring-oauth/.project | 47 ++ spring-oauth/.springBeans | 15 + spring-oauth/pom.xml | 425 ++++++++++++++++++ .../baeldung/config/ServletInitializer.java | 42 ++ .../java/org/baeldung/config/WebConfig.java | 94 ++++ .../org/baeldung/web/RedditController.java | 28 ++ spring-oauth/src/main/resources/log4j.xml | 41 ++ .../src/main/resources/reddit.properties | 4 + .../src/main/webapp/WEB-INF/jsp/reddit.jsp | 12 + spring-oauth/src/main/webapp/index.jsp | 15 + spring-oauth/src/test/resources/log4j.xml | 41 ++ 12 files changed, 793 insertions(+) create mode 100644 spring-oauth/.classpath create mode 100644 spring-oauth/.project create mode 100644 spring-oauth/.springBeans create mode 100644 spring-oauth/pom.xml create mode 100644 spring-oauth/src/main/java/org/baeldung/config/ServletInitializer.java create mode 100644 spring-oauth/src/main/java/org/baeldung/config/WebConfig.java create mode 100644 spring-oauth/src/main/java/org/baeldung/web/RedditController.java create mode 100644 spring-oauth/src/main/resources/log4j.xml create mode 100644 spring-oauth/src/main/resources/reddit.properties create mode 100755 spring-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp create mode 100755 spring-oauth/src/main/webapp/index.jsp create mode 100644 spring-oauth/src/test/resources/log4j.xml diff --git a/spring-oauth/.classpath b/spring-oauth/.classpath new file mode 100644 index 0000000000..c3ce2e4d45 --- /dev/null +++ b/spring-oauth/.classpath @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-oauth/.project b/spring-oauth/.project new file mode 100644 index 0000000000..92c5182eb6 --- /dev/null +++ b/spring-oauth/.project @@ -0,0 +1,47 @@ + + + spring-oauth + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/spring-oauth/.springBeans b/spring-oauth/.springBeans new file mode 100644 index 0000000000..696dd47808 --- /dev/null +++ b/spring-oauth/.springBeans @@ -0,0 +1,15 @@ + + + 1 + + + + + + + + + + + + diff --git a/spring-oauth/pom.xml b/spring-oauth/pom.xml new file mode 100644 index 0000000000..457b8f9c3f --- /dev/null +++ b/spring-oauth/pom.xml @@ -0,0 +1,425 @@ + + 4.0.0 + org.baeldung + spring-oauth + 0.1-SNAPSHOT + + spring-oauth + war + + + + + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + + org.springframework.security.oauth + spring-security-oauth2 + 2.0.6.RELEASE + + + + + + org.springframework + spring-core + ${org.springframework.version} + + + commons-logging + commons-logging + + + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-jdbc + ${org.springframework.version} + + + org.springframework + spring-beans + ${org.springframework.version} + + + org.springframework + spring-aop + ${org.springframework.version} + + + org.springframework + spring-tx + ${org.springframework.version} + + + org.springframework + spring-expression + ${org.springframework.version} + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + + + + + + org.springframework + spring-orm + ${org.springframework.version} + + + org.springframework.data + spring-data-jpa + ${spring-data-jpa.version} + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + xml-apis + xml-apis + 1.4.01 + + + org.javassist + javassist + 3.18.2-GA + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + runtime + + + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + javax.servlet + jstl + 1.2 + runtime + + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.thoughtworks.xstream + xstream + 1.4.7 + + + + + + com.google.guava + guava + ${guava.version} + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + com.jayway.restassured + rest-assured + 2.3.0 + test + + + commons-logging + commons-logging + + + + + + + + spring-oauth + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8080 + + + + + + + + + + + + integration + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + json + + + + + org.codehaus.cargo + cargo-maven2-plugin + + false + + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + + + + + + + + + + 4.1.4.RELEASE + 3.2.5.RELEASE + + + 4.3.7.Final + 5.1.34 + 1.7.1.RELEASE + + + + 2.4.4 + + + 1.7.9 + 1.1.2 + + + 5.1.3.Final + + + 18.0 + 3.3.2 + + + 1.3 + 4.11 + 1.10.19 + + 4.4 + 4.3.6 + + 2.4.0 + + + 3.2 + 2.5 + 2.18.1 + 1.4.11 + + + + \ No newline at end of file diff --git a/spring-oauth/src/main/java/org/baeldung/config/ServletInitializer.java b/spring-oauth/src/main/java/org/baeldung/config/ServletInitializer.java new file mode 100644 index 0000000000..a5b8708ffa --- /dev/null +++ b/spring-oauth/src/main/java/org/baeldung/config/ServletInitializer.java @@ -0,0 +1,42 @@ +package org.baeldung.config; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.DelegatingFilterProxy; +import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; + +public class ServletInitializer extends AbstractDispatcherServletInitializer { + + @Override + protected WebApplicationContext createServletApplicationContext() { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(WebConfig.class); + return context; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected WebApplicationContext createRootApplicationContext() { + return null; + } + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + super.onStartup(servletContext); + registerProxyFilter(servletContext, "oauth2ClientContextFilter"); + } + + private void registerProxyFilter(ServletContext servletContext, String name) { + DelegatingFilterProxy filter = new DelegatingFilterProxy(name); + filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher"); + servletContext.addFilter(name, filter).addMappingForUrlPatterns(null, false, "/*"); + } + +} diff --git a/spring-oauth/src/main/java/org/baeldung/config/WebConfig.java b/spring-oauth/src/main/java/org/baeldung/config/WebConfig.java new file mode 100644 index 0000000000..2bd2fcad43 --- /dev/null +++ b/spring-oauth/src/main/java/org/baeldung/config/WebConfig.java @@ -0,0 +1,94 @@ +package org.baeldung.config; + +import java.util.Arrays; + +import org.baeldung.web.RedditController; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.security.oauth2.client.OAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; +import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; +import org.springframework.web.client.RestOperations; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@EnableWebMvc +public class WebConfig extends WebMvcConfigurerAdapter { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/WEB-INF/jsp/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Bean + public RedditController redditController(@Qualifier("redditRestTemplate") RestOperations redditRestTemplate) { + RedditController controller = new RedditController(); + controller.setRedditRestTemplate(redditRestTemplate); + return controller; + } + + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Configuration + @EnableOAuth2Client + @PropertySource("classpath:reddit.properties") + protected static class ResourceConfiguration { + + @Value("${accessTokenUri}") + private String accessTokenUri; + + @Value("${userAuthorizationUri}") + private String userAuthorizationUri; + + @Value("${clientID}") + private String clientID; + + @Value("${clientSecret}") + private String clientSecret; + + @Bean + public OAuth2ProtectedResourceDetails reddit() { + AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); + details.setId("reddit"); + details.setClientId(clientID); + details.setClientSecret(clientSecret); + details.setAccessTokenUri(accessTokenUri); + details.setUserAuthorizationUri(userAuthorizationUri); + details.setTokenName("oauth_token"); + details.setScope(Arrays.asList("identity")); + return details; + } + + @Bean + public OAuth2RestTemplate redditRestTemplate(OAuth2ClientContext clientContext) { + OAuth2RestTemplate template = new OAuth2RestTemplate(reddit(), clientContext); + return template; + } + } +} \ No newline at end of file diff --git a/spring-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-oauth/src/main/java/org/baeldung/web/RedditController.java new file mode 100644 index 0000000000..31a1e9a920 --- /dev/null +++ b/spring-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -0,0 +1,28 @@ +package org.baeldung.web; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.client.RestOperations; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Controller +public class RedditController { + + private RestOperations redditRestTemplate; + + @RequestMapping("/reddit/info") + public String getInfo(Model model) throws Exception { + String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", String.class); + JsonNode node = new ObjectMapper().readTree(result); + model.addAttribute("info", node.get("name").asText()); + return "reddit"; + } + + public void setRedditRestTemplate(RestOperations redditRestTemplate) { + this.redditRestTemplate = redditRestTemplate; + } + +} \ No newline at end of file diff --git a/spring-oauth/src/main/resources/log4j.xml b/spring-oauth/src/main/resources/log4j.xml new file mode 100644 index 0000000000..090fbe7dce --- /dev/null +++ b/spring-oauth/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-oauth/src/main/resources/reddit.properties b/spring-oauth/src/main/resources/reddit.properties new file mode 100644 index 0000000000..b850bf8e4a --- /dev/null +++ b/spring-oauth/src/main/resources/reddit.properties @@ -0,0 +1,4 @@ +clientID=5iAHD5-BBmEL8Q +clientSecret=grkII6-THVcFsMm1wVTiQDRONmw +accessTokenUri=https://www.reddit.com/api/v1/access_token +userAuthorizationUri=https://www.reddit.com/api/v1/authorize diff --git a/spring-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp b/spring-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp new file mode 100755 index 0000000000..5e17c3904c --- /dev/null +++ b/spring-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp @@ -0,0 +1,12 @@ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +Spring OAuth + + +

Your Reddit Info

+Your reddit username is ${info} + + \ No newline at end of file diff --git a/spring-oauth/src/main/webapp/index.jsp b/spring-oauth/src/main/webapp/index.jsp new file mode 100755 index 0000000000..06baaf8e3c --- /dev/null +++ b/spring-oauth/src/main/webapp/index.jsp @@ -0,0 +1,15 @@ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + + +Spring OAuth + + + +

Welcome to Spring OAuth

+Login with Reddit + + + \ No newline at end of file diff --git a/spring-oauth/src/test/resources/log4j.xml b/spring-oauth/src/test/resources/log4j.xml new file mode 100644 index 0000000000..d867d97431 --- /dev/null +++ b/spring-oauth/src/test/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +