diff --git a/spring-security-oauth/Procfile b/spring-security-oauth/Procfile new file mode 100644 index 0000000000..a9162cdc9c --- /dev/null +++ b/spring-security-oauth/Procfile @@ -0,0 +1 @@ +web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war diff --git a/spring-security-oauth/pom.xml b/spring-security-oauth/pom.xml index 3329da4575..c6056e72a0 100644 --- a/spring-security-oauth/pom.xml +++ b/spring-security-oauth/pom.xml @@ -106,6 +106,26 @@ ${httpcore.version} + + + + org.thymeleaf + thymeleaf + 2.1.4.RELEASE + + + + org.thymeleaf + thymeleaf-spring4 + 2.1.4.RELEASE + + + + org.thymeleaf.extras + thymeleaf-extras-springsecurity3 + 2.1.2.RELEASE + + @@ -140,6 +160,14 @@ runtime + + + + com.h2database + h2 + 1.4.187 + + @@ -328,6 +356,29 @@ + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.3 + + + package + copy + + + + com.github.jsimone + webapp-runner + 7.0.57.2 + webapp-runner.jar + + + + + + diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/SecurityConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/SecurityConfig.java index cdfccb99e6..5159031adc 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/SecurityConfig.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/SecurityConfig.java @@ -34,7 +34,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .httpBasic().authenticationEntryPoint(oauth2AuthenticationEntryPoint()) .and() .logout() - .deleteCookies("JSESSIONID","CustomRememberMe") + .deleteCookies("JSESSIONID") .logoutUrl("/logout") .logoutSuccessUrl("/"); // @formatter:on diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/ServletInitializer.java b/spring-security-oauth/src/main/java/org/baeldung/config/ServletInitializer.java index 554bf32dde..471c7fc3ef 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/ServletInitializer.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/ServletInitializer.java @@ -13,7 +13,7 @@ public class ServletInitializer extends AbstractDispatcherServletInitializer { @Override protected WebApplicationContext createServletApplicationContext() { final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(PersistenceJPAConfig.class, WebConfig.class, SecurityConfig.class); + context.register(PersistenceJPAConfig.class, WebConfig.class, SecurityConfig.class, ThymeleafConfig.class); return context; } diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/SessionListener.java b/spring-security-oauth/src/main/java/org/baeldung/config/SessionListener.java index 96cf6058e4..f2fba5a99d 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/SessionListener.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/SessionListener.java @@ -13,7 +13,7 @@ public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { logger.info("==== Session is created ===="); - event.getSession().setMaxInactiveInterval(30 * 60); + event.getSession().setMaxInactiveInterval(60 * 60); event.getSession().setAttribute("PREDICTION_FEATURE", MyFeatures.PREDICTION_FEATURE); } diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/ThymeleafConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/ThymeleafConfig.java new file mode 100644 index 0000000000..63d9c4b177 --- /dev/null +++ b/spring-security-oauth/src/main/java/org/baeldung/config/ThymeleafConfig.java @@ -0,0 +1,39 @@ +package org.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect; +import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; +import org.thymeleaf.templateresolver.TemplateResolver; + +@Configuration +public class ThymeleafConfig { + @Bean + public TemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/jsp/"); + templateResolver.setSuffix(".jsp"); + + return templateResolver; + } + + @Bean + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + templateEngine.addDialect(new SpringSecurityDialect()); + return templateEngine; + } + + @Bean + public ViewResolver viewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + + return viewResolver; + } +} diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java index c8ba494663..38adad0b8a 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java @@ -5,7 +5,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.baeldung.persistence.service.RedditTokenService; +import javax.annotation.PostConstruct; + +import org.apache.commons.lang.StringUtils; +import org.baeldung.persistence.dao.PostRepository; import org.baeldung.reddit.classifier.RedditClassifier; import org.baeldung.reddit.util.MyFeatures; import org.baeldung.reddit.util.UserAgentInterceptor; @@ -115,6 +118,9 @@ public class WebConfig extends WebMvcConfigurerAdapter { @Value("${clientSecret}") private String clientSecret; + @Autowired + private PostRepository repo; + @Bean public OAuth2ProtectedResourceDetails reddit() { final AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); @@ -132,16 +138,24 @@ public class WebConfig extends WebMvcConfigurerAdapter { } @Bean - public OAuth2RestTemplate redditRestTemplate(OAuth2ClientContext clientContext, RedditTokenService redditTokenService) { + public OAuth2RestTemplate redditRestTemplate(OAuth2ClientContext clientContext) { final OAuth2RestTemplate template = new OAuth2RestTemplate(reddit(), clientContext); final List list = new ArrayList(); list.add(new UserAgentInterceptor()); template.setInterceptors(list); final AccessTokenProviderChain accessTokenProvider = new AccessTokenProviderChain(Arrays. asList(new MyAuthorizationCodeAccessTokenProvider(), new ImplicitAccessTokenProvider(), new ResourceOwnerPasswordAccessTokenProvider(), new ClientCredentialsAccessTokenProvider())); - accessTokenProvider.setClientTokenServices(redditTokenService); template.setAccessTokenProvider(accessTokenProvider); return template; } + + @PostConstruct + public void startupCheck() { + if (StringUtils.isBlank(accessTokenUri) || StringUtils.isBlank(userAuthorizationUri) || StringUtils.isBlank(clientID) || StringUtils.isBlank(clientSecret)) { + throw new RuntimeException("Incomplete reddit properties"); + } + repo.findAll(); + } + } } diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/UserRepository.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/UserRepository.java index 4b045aa37c..ec2221c73d 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/UserRepository.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/dao/UserRepository.java @@ -8,6 +8,4 @@ public interface UserRepository extends JpaRepository { User findByUsername(String username); User findByAccessToken(String token); - - User findByRememberMeToken(String token); } \ No newline at end of file diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java index d47fe067d2..e3b5553922 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java @@ -26,8 +26,6 @@ public class User { private boolean needCaptcha; - private String rememberMeToken; - public User() { super(); } @@ -82,14 +80,6 @@ public class User { // - public String getRememberMeToken() { - return rememberMeToken; - } - - public void setRememberMeToken(String rememberMeToken) { - this.rememberMeToken = rememberMeToken; - } - @Override public int hashCode() { final int prime = 31; diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/service/RedditTokenService.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/service/RedditTokenService.java deleted file mode 100644 index 5a20fff1de..0000000000 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/service/RedditTokenService.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.model.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.Authentication; -import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; -import org.springframework.security.oauth2.client.token.ClientTokenServices; -import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; -import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken; -import org.springframework.security.oauth2.common.OAuth2AccessToken; -import org.springframework.stereotype.Component; - -@Component -public class RedditTokenService implements ClientTokenServices { - - @Autowired - private UserRepository userReopsitory; - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - public RedditTokenService() { - super(); - } - - @Override - public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) { - logger.info("reddit ==== getAccessToken"); - final User user = (User) authentication.getPrincipal(); - final DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(user.getAccessToken()); - token.setRefreshToken(new DefaultOAuth2RefreshToken((user.getRefreshToken()))); - token.setExpiration(user.getTokenExpiration()); - return token; - } - - @Override - public void saveAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication, OAuth2AccessToken accessToken) { - logger.info("reddit ==== saveAccessToken"); - final User user = (User) authentication.getPrincipal(); - user.setAccessToken(accessToken.getValue()); - if (accessToken.getRefreshToken() != null) { - user.setRefreshToken(accessToken.getRefreshToken().getValue()); - } - user.setTokenExpiration(accessToken.getExpiration()); - userReopsitory.save(user); - } - - @Override - public void removeAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) { - logger.info("reddit ==== removeAccessToken"); - } - -} diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 672749a99e..585d235aea 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -8,11 +8,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang.RandomStringUtils; import org.baeldung.persistence.dao.PostRepository; import org.baeldung.persistence.dao.UserRepository; import org.baeldung.persistence.model.Post; @@ -33,7 +28,6 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -50,7 +44,6 @@ public class RedditController { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private final SimpleDateFormat dfHour = new SimpleDateFormat("HH"); - public static final String REMEMBER_ME_COOKIE = "CustomRememberMe"; @Autowired @Qualifier("redditRestTemplate") @@ -66,11 +59,9 @@ public class RedditController { private RedditClassifier redditClassifier; @RequestMapping("/login") - public final String redditLogin(@CookieValue(value = REMEMBER_ME_COOKIE, required = false) String rememberMe, HttpServletRequest request, HttpServletResponse response) { - if (!canAutoLogin(rememberMe)) { - final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class); - loadAuthentication(node.get("name").asText(), redditRestTemplate.getAccessToken(), response); - } + public final String redditLogin() { + final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class); + loadAuthentication(node.get("name").asText(), redditRestTemplate.getAccessToken()); return "redirect:home.html"; } @@ -229,14 +220,14 @@ public class RedditController { return result; } else { if ((node.get("json").get("data") != null) && (node.get("json").get("data").get("url") != null)) { - return "Post submitted successfully check it out "; + return "Post submitted successfully " + node.get("json").get("data").get("url").asText(); } else { return "Error Occurred"; } } } - private void loadAuthentication(final String name, final OAuth2AccessToken token, HttpServletResponse response) { + private void loadAuthentication(final String name, final OAuth2AccessToken token) { User user = userReopsitory.findByUsername(name); if (user == null) { user = new User(); @@ -254,35 +245,8 @@ public class RedditController { user.setTokenExpiration(token.getExpiration()); userReopsitory.save(user); - generateRememberMeToken(user, response); - final UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, token.getValue(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))); SecurityContextHolder.getContext().setAuthentication(auth); } - private void generateRememberMeToken(User user, HttpServletResponse response) { - String rememberMe = RandomStringUtils.randomAlphanumeric(30); - while (userReopsitory.findByRememberMeToken(rememberMe) != null) { - rememberMe = RandomStringUtils.randomAlphanumeric(30); - } - user.setRememberMeToken(rememberMe); - userReopsitory.save(user); - final Cookie c = new Cookie(REMEMBER_ME_COOKIE, rememberMe); - c.setMaxAge(1209600); - response.addCookie(c); - } - - private boolean canAutoLogin(String rememberMeToken) { - if (rememberMeToken != null) { - final User user = userReopsitory.findByRememberMeToken(rememberMeToken); - if (user != null) { - logger.info("Auto Login successfully"); - final UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getAccessToken(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))); - SecurityContextHolder.getContext().setAuthentication(auth); - return true; - } - } - return false; - } - } diff --git a/spring-security-oauth/src/main/resources/persistence-prod.properties b/spring-security-oauth/src/main/resources/persistence-prod.properties index 3948c3a7fb..856c4ce00c 100644 --- a/spring-security-oauth/src/main/resources/persistence-prod.properties +++ b/spring-security-oauth/src/main/resources/persistence-prod.properties @@ -1,10 +1,10 @@ ################### DataSource Configuration ########################## -jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/oauth_reddit?createDatabaseIfNotExist=true -jdbc.user=tutorialuser -jdbc.pass=tutorialmy5ql +jdbc.driverClassName=org.postgresql.Driver +jdbc.url=jdbc:postgres://fkqrbxdwqbrzhv:Y83Axvgz5iC_tyRZQxI0KEmUEG@ec2-107-20-222-114.compute-1.amazonaws.com:5432/dfmoej36meltnj +jdbc.user=fkqrbxdwqbrzhv +jdbc.pass=Y83Axvgz5iC_tyRZQxI0KEmUEG ################### Hibernate Configuration ########################## -hibernate.dialect=org.hibernate.dialect.MySQLDialect +hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=false hibernate.hbm2ddl.auto=update diff --git a/spring-security-oauth/src/main/resources/persistence-test.properties b/spring-security-oauth/src/main/resources/persistence-test.properties index d6400c9dfc..929f922d1e 100644 --- a/spring-security-oauth/src/main/resources/persistence-test.properties +++ b/spring-security-oauth/src/main/resources/persistence-test.properties @@ -1,10 +1,10 @@ ################### DataSource Configuration ########################## -jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/oauth_reddit?createDatabaseIfNotExist=true -jdbc.user=tutorialuser -jdbc.pass=tutorialmy5ql - +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:oauth_reddit;DB_CLOSE_DELAY=-1 +jdbc.user=sa +jdbc.pass= +init-db=false ################### Hibernate Configuration ########################## -hibernate.dialect=org.hibernate.dialect.MySQLDialect +hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.show_sql=false -hibernate.hbm2ddl.auto=create-drop +hibernate.hbm2ddl.auto=update diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp index d42c33c24d..8a0f574c82 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp @@ -1,13 +1,12 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> Schedule to Reddit - -"> + + - - + + - +

Edit Scheduled Post

-
" method="post" role="form" data-toggle="validator"> +
- +
-

+

- +
-

+

- +
-

+

- checked /> + + + + +
-

+

- + -

+

diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/header.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/header.jsp index 1313bb456b..e6805c71ef 100644 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/header.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/header.jsp @@ -1,24 +1,23 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> - + \ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/home.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/home.jsp index fdeec894ea..af79be0da9 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/home.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/home.jsp @@ -1,18 +1,16 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> Schedule to Reddit - + - +
-

Welcome,

-
+

Welcome, Bob

+
My Scheduled Posts Post to Reddit Schedule Post to Reddit diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp index f6d8ac230e..ae0058b372 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp @@ -1,16 +1,12 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> - Schedule to Reddit - + - +

My Scheduled Posts

@@ -23,21 +19,20 @@ Actions - - class="success"> - - - + + + + - Edit - Delete + Edit + Delete -
\ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp index 7d007cef68..41c0180843 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp @@ -1,16 +1,13 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> - Schedule to Reddit - -"> + + - - + + - +

Schedule Post to Reddit

@@ -32,36 +29,36 @@ border-color: #ddd;
- +
-

+

- +
-

+

- +
-

+

-
+

-
+
Votes didn't exceed - + within    - + @@ -81,18 +78,20 @@ border-color: #ddd;
-

+

- + -

+

diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp index 612d285504..a003186f73 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp @@ -1,13 +1,10 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> - Schedule to Reddit - + - + - +

Post to Reddit

@@ -29,45 +26,46 @@ border-color: #ddd;
- +
-

+

- +
-

+

- +
-

+

-

+

- +
-

+

captcha - -

+
+

- +
- +
diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp index f4226c0aa4..9de779ea9d 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp @@ -1,17 +1,14 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> - Schedule to Reddit - + - +
-

${msg}

+

Hello

\ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/index.jsp b/spring-security-oauth/src/main/webapp/index.jsp index 2c90a27733..51c283654a 100755 --- a/spring-security-oauth/src/main/webapp/index.jsp +++ b/spring-security-oauth/src/main/webapp/index.jsp @@ -1,11 +1,9 @@ - -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> Schedule to Reddit - +
diff --git a/spring-security-oauth/src/test/java/org/baeldung/classifier/RedditClassifierTest.java b/spring-security-oauth/src/test/java/org/baeldung/classifier/RedditClassifierTest.java index d72a689253..6b51c852c7 100644 --- a/spring-security-oauth/src/test/java/org/baeldung/classifier/RedditClassifierTest.java +++ b/spring-security-oauth/src/test/java/org/baeldung/classifier/RedditClassifierTest.java @@ -6,9 +6,10 @@ import java.io.IOException; import org.baeldung.reddit.classifier.RedditClassifier; import org.baeldung.reddit.classifier.RedditDataCollector; +import org.junit.Ignore; import org.junit.Test; -//@Ignore +@Ignore public class RedditClassifierTest { @Test diff --git a/spring-security-oauth/src/test/java/org/baeldung/persistence/PersistenceJPATest.java b/spring-security-oauth/src/test/java/org/baeldung/persistence/PersistenceJPATest.java index a025d171b3..9b6591497c 100644 --- a/spring-security-oauth/src/test/java/org/baeldung/persistence/PersistenceJPATest.java +++ b/spring-security-oauth/src/test/java/org/baeldung/persistence/PersistenceJPATest.java @@ -8,13 +8,11 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; -import org.baeldung.config.PersistenceJPAConfig; import org.baeldung.persistence.dao.PostRepository; import org.baeldung.persistence.dao.UserRepository; import org.baeldung.persistence.model.Post; import org.baeldung.persistence.model.User; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -24,10 +22,10 @@ import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceJPAConfig.class }) +@ContextConfiguration(classes = { TestJPAConfig.class }) @Transactional @TransactionConfiguration -@Ignore +// @Ignore public class PersistenceJPATest { @Autowired private PostRepository postRepository; diff --git a/spring-security-oauth/src/test/java/org/baeldung/persistence/TestJPAConfig.java b/spring-security-oauth/src/test/java/org/baeldung/persistence/TestJPAConfig.java new file mode 100644 index 0000000000..a3d29d7424 --- /dev/null +++ b/spring-security-oauth/src/test/java/org/baeldung/persistence/TestJPAConfig.java @@ -0,0 +1,73 @@ +package org.baeldung.persistence; + +import java.util.Properties; + +import javax.sql.DataSource; + +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.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-test.properties" }) +@ComponentScan({ "org.baeldung.persistence.model", "org.baeldung.persistence.dao" }) +@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") +public class TestJPAConfig { + @Autowired + private Environment env; + + public TestJPAConfig() { + super(); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + em.setJpaVendorAdapter(vendorAdapter); + em.setJpaProperties(additionalProperties()); + return em; + } + + @Bean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.user")); + dataSource.setPassword(env.getProperty("jdbc.pass")); + return dataSource; + } + + @Bean + public JpaTransactionManager transactionManager() { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto", "create-drop")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + return hibernateProperties; + } +}