├── .gitignore ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── config │ └── ci │ │ └── gradle.properties ├── java │ └── com │ │ └── porterhead │ │ └── rest │ │ ├── api │ │ ├── ErrorResponse.java │ │ ├── PagedQueryRequest.java │ │ ├── PagedResponse.java │ │ └── ValidationError.java │ │ ├── authorization │ │ ├── AuthorizationRequestContext.java │ │ ├── AuthorizationService.java │ │ ├── exception │ │ │ └── InvalidAuthorizationHeaderException.java │ │ └── impl │ │ │ ├── RequestSigningAuthorizationService.java │ │ │ ├── SecurityContextImpl.java │ │ │ └── SessionTokenAuthorizationService.java │ │ ├── config │ │ ├── ApplicationConfig.java │ │ ├── ApplicationDevConfig.java │ │ ├── ApplicationProductionConfig.java │ │ └── ApplicationStagingConfig.java │ │ ├── exception │ │ ├── ApplicationRuntimeException.java │ │ ├── BaseWebApplicationException.java │ │ ├── NotFoundException.java │ │ └── ValidationException.java │ │ ├── filter │ │ ├── ResourceFilterFactory.java │ │ └── SecurityContextFilter.java │ │ ├── gateway │ │ └── EmailServicesGateway.java │ │ ├── model │ │ └── BaseEntity.java │ │ ├── resource │ │ ├── GenericExceptionMapper.java │ │ └── HealthCheckResource.java │ │ ├── service │ │ └── BaseService.java │ │ ├── user │ │ ├── EmailServiceTokenModel.java │ │ ├── SocialUserRepository.java │ │ ├── UserRepository.java │ │ ├── UserService.java │ │ ├── UserServiceImpl.java │ │ ├── VerificationTokenRepository.java │ │ ├── VerificationTokenService.java │ │ ├── VerificationTokenServiceImpl.java │ │ ├── api │ │ │ ├── AuthenticatedUserToken.java │ │ │ ├── CreateUserRequest.java │ │ │ ├── EmailVerificationRequest.java │ │ │ ├── ExternalUser.java │ │ │ ├── LoginRequest.java │ │ │ ├── LostPasswordRequest.java │ │ │ ├── OAuth2Request.java │ │ │ ├── PasswordRequest.java │ │ │ ├── SocialProfile.java │ │ │ └── UpdateUserRequest.java │ │ ├── domain │ │ │ ├── AuthorizationToken.java │ │ │ ├── Role.java │ │ │ ├── SocialUser.java │ │ │ ├── SocialUserBuilder.java │ │ │ ├── User.java │ │ │ └── VerificationToken.java │ │ ├── exception │ │ │ ├── AlreadyVerifiedException.java │ │ │ ├── AuthenticationException.java │ │ │ ├── AuthorizationException.java │ │ │ ├── DuplicateUserException.java │ │ │ ├── TokenHasExpiredException.java │ │ │ ├── TokenNotFoundException.java │ │ │ └── UserNotFoundException.java │ │ ├── mail │ │ │ ├── MailSenderService.java │ │ │ ├── MockJavaMailSender.java │ │ │ └── impl │ │ │ │ └── MailSenderServiceImpl.java │ │ ├── resource │ │ │ ├── PasswordResource.java │ │ │ ├── UserResource.java │ │ │ └── VerificationResource.java │ │ └── social │ │ │ ├── JpaConnectionRepository.java │ │ │ ├── JpaUsersConnectionRepository.java │ │ │ └── SocialConfig.java │ │ └── util │ │ ├── DateUtil.java │ │ ├── HashUtil.java │ │ └── StringUtil.java ├── resources │ ├── META-INF │ │ ├── persistence.xml │ │ ├── spring │ │ │ ├── component-scan-context.xml │ │ │ ├── data-context.xml │ │ │ ├── email-services-context.xml │ │ │ ├── email-template-context.xml │ │ │ ├── root-context.xml │ │ │ └── social-configuration-context.xml │ │ └── velocity │ │ │ ├── LostPasswordEmail.vm │ │ │ ├── RegistrationEmail.vm │ │ │ └── VerifyEmail.vm │ ├── logback.xml │ ├── properties │ │ ├── app.properties │ │ ├── dev-app.properties │ │ ├── production-app.properties │ │ └── staging-app.properties │ └── schema │ │ ├── indexes.sql │ │ ├── message_store.sql │ │ └── truncate_data.sql └── webapp │ ├── META-INF │ └── MANIFEST.MF │ ├── WEB-INF │ ├── spring │ │ └── appservlet │ │ │ └── servlet-context.xml │ └── web.xml │ ├── css │ └── styles.css │ ├── dashboard.html │ ├── forgot_password.html │ ├── img │ ├── fb_image.png │ └── fbline.png │ ├── index.html │ ├── js │ ├── bootstrap.js │ ├── bootstrap.min.js │ ├── cookie.js │ ├── enc-base64-min.js │ ├── grid.locale-en.js │ ├── javarest.js │ ├── jquery-1.7.1.min.js │ ├── jquery-1.8.2.min.js │ ├── jquery-full-house.js │ ├── jquery.jqGrid.min.js │ ├── sha256.js │ ├── store.js │ ├── user.js │ └── verify.js │ ├── request_email.html │ ├── reset_password.html │ ├── signup.html │ └── validate.html └── test ├── groovy ├── BaseIntegrationTst.groovy └── UserIntegrationTest.groovy ├── java └── com │ └── porterhead │ └── rest │ ├── authorization │ ├── BaseAuthorizationTst.java │ ├── RequestSigningAuthorizationServiceTest.java │ ├── SecurityContextTest.java │ └── SessionTokenAuthorizationServiceTest.java │ ├── filter │ └── SecurityContextFilterTest.java │ ├── mock │ └── AppMockConfiguration.java │ ├── resource │ ├── BaseResourceTst.java │ ├── ConsumerSimpleSecurityFilter.java │ ├── HealthCheckResourceTest.java │ └── SimpleSecurityFilter.java │ └── user │ ├── BaseServiceTest.java │ ├── MailSenderServiceTest.java │ ├── UserServiceTest.java │ ├── VerificationServiceTest.java │ ├── api │ ├── CreateUserRequestTest.java │ ├── LoginRequestTest.java │ ├── PasswordRequestTest.java │ └── ValidationTst.java │ ├── builder │ └── ExternalUserBuilder.java │ ├── resource │ ├── PasswordResourceTest.java │ ├── UserResourceTest.java │ └── VerificationResourceTest.java │ └── social │ ├── AbstractSocialTst.java │ ├── JpaConnectionRepositoryTest.java │ └── JpaUsersConnectionRepositoryTest.java └── resources ├── integration-test-context.xml └── social-test-context.xml /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | 4 | .gradle/* 5 | 6 | build/* 7 | 8 | out/* 9 | 10 | *.iml 11 | 12 | *.ipr 13 | 14 | *.iws 15 | /build/ 16 | /bin/ 17 | 18 | # eclipse 19 | .settings/ 20 | .project 21 | .classpath 22 | 23 | # Intellij 24 | .idea/ 25 | *.iml 26 | *.iws 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JAVA REST Application 2 | ==================== 3 | 4 | Simple and easily understandable web project that demonstrates the use of: 5 | 6 | * Jersey + JAX-RS 7 | * Spring Integration 8 | * Spring Data + Hibernate 9 | * Groovy Integration tests 10 | * OAuth 11 | * Velocity + Java Mail 12 | * Facebook Login 13 | * Password Reset 14 | * Login/Sign Up + Email Verification 15 | * JSR 303 Validation 16 | 17 | NOTE. For a similar project that uses most of the same components but is built around OAuth2 see 18 | Securing Rest Services with OAuth2 and Spring Security 19 | 20 | to build: 21 | 22 | gradle clean build integrationTest 23 | 24 | or use the gradle wrapper: 25 | 26 | ./gradlew clean build integrationTest 27 | 28 | go to /build/reports/emma for test coverage reports 29 | 30 | to run: 31 | 32 | gradle tomcatRun 33 | 34 | navigate to http://localhost:8080/java-rest/ 35 | 36 | see blog posts: 37 | THANK YOU 38 | 39 |
19 | *
20 | com.sun.jersey.spi.container.ResourceFilters
21 | com.porterhead.com.porterhead.rest.filter.ResourceFilterFactory
22 |
23 | *
24 | *
25 | *
26 | * @author: Iain Porter
27 | */
28 | @Component
29 | @Provider
30 | public class ResourceFilterFactory extends RolesAllowedResourceFilterFactory {
31 |
32 | @Autowired
33 | private SecurityContextFilter securityContextFilter;
34 |
35 | @Override
36 | public List
59 | * /user/1883c578-76be-47fb-a5c1-7bbea3bf7fd0 using uuid as the identifier
60 | *
61 | * /user/jsmith using the username as the identifier
62 | *
63 | *
64 | *
65 | * @return Object unique identifier for the object
66 | */
67 | public Object getIdentifier() {
68 | return getUuid().toString();
69 | }
70 |
71 | public int getVersion() {
72 | return version;
73 | }
74 |
75 | public Date getTimeCreated() {
76 | return timeCreated;
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/com/porterhead/rest/resource/GenericExceptionMapper.java:
--------------------------------------------------------------------------------
1 | package com.porterhead.rest.resource;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import javax.ws.rs.WebApplicationException;
7 | import javax.ws.rs.core.Response;
8 | import javax.ws.rs.ext.ExceptionMapper;
9 | import javax.ws.rs.ext.Provider;
10 |
11 | /**
12 | * User: porter
13 | * Date: 22/03/2012
14 | * Time: 15:56
15 | */
16 | @Provider
17 | public class GenericExceptionMapper implements ExceptionMapper
10 |
11 |
|
9 |
10 |
|
10 |
11 |
|