├── spring.security.custom.rest.api ├── target │ ├── maven-status │ │ └── maven-compiler-plugin │ │ │ ├── testCompile │ │ │ └── default-testCompile │ │ │ │ └── inputFiles.lst │ │ │ └── compile │ │ │ └── default-compile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ ├── maven-archiver │ │ └── pom.properties │ ├── spring-security-custom-rest-api │ │ └── WEB-INF │ │ │ ├── api-servlet.xml │ │ │ ├── classes │ │ │ ├── logback.xml │ │ │ └── webSecurityConfig.xml │ │ │ └── web.xml │ └── classes │ │ ├── logback.xml │ │ └── webSecurityConfig.xml ├── .DS_Store ├── src │ ├── .DS_Store │ └── main │ │ ├── webapp │ │ └── WEB-INF │ │ │ ├── api-servlet.xml │ │ │ └── web.xml │ │ ├── resources │ │ ├── logback.xml │ │ └── webSecurityConfig.xml │ │ └── java │ │ └── spring │ │ └── security │ │ └── custom │ │ └── rest │ │ └── api │ │ ├── SpringSecurityConfig.java │ │ ├── WebConfig.java │ │ ├── entity │ │ └── CustomerDetails.java │ │ ├── security │ │ ├── RestAuthenticationAccessDeniedHandler.java │ │ ├── RestAuthenticationEntryPoint.java │ │ ├── RestAuthenticationSuccessHandler.java │ │ ├── CustomUserData.java │ │ └── CustomUserDetailsService.java │ │ └── service │ │ └── CustomerDetailService.java ├── .settings │ ├── org.eclipse.m2e.core.prefs │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.jdt.core.prefs ├── .classpath ├── .project └── pom.xml ├── spring.security.rest.api ├── src │ ├── .DS_Store │ └── main │ │ ├── webapp │ │ └── WEB-INF │ │ │ ├── api-servlet.xml │ │ │ └── web.xml │ │ ├── resources │ │ ├── logback.xml │ │ └── webSecurityConfig.xml │ │ └── java │ │ └── spring │ │ └── security │ │ └── rest │ │ └── api │ │ ├── SpringSecurityConfig.java │ │ ├── WebConfig.java │ │ ├── entity │ │ └── CustomerDetails.java │ │ ├── security │ │ ├── RestAuthenticationEntryPoint.java │ │ └── RestAuthenticationSuccessHandler.java │ │ └── service │ │ └── CustomerDetailService.java └── pom.xml └── .gitignore /spring.security.custom.rest.api/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/malalanayake/spring-security/HEAD/spring.security.rest.api/src/.DS_Store -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/malalanayake/spring-security/HEAD/spring.security.custom.rest.api/.DS_Store -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/malalanayake/spring-security/HEAD/spring.security.custom.rest.api/src/.DS_Store -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Sun Jun 29 23:45:06 CDT 2014 3 | version=0.0.1-SNAPSHOT 4 | groupId=spring 5 | artifactId=spring.security.custom.rest.api 6 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/webapp/WEB-INF/api-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/webapp/WEB-INF/api-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/spring-security-custom-rest-api/WEB-INF/api-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | security.rest.api 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/classes/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | web - %date [%thread] %-5level %logger{36} - %message%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | web - %date [%thread] %-5level %logger{36} - %message%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | web - %date [%thread] %-5level %logger{36} - %message%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/SpringSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.ImportResource; 6 | 7 | 8 | /** 9 | * Expose the Spring Security Configuration 10 | * 11 | * @author malalanayake 12 | * 13 | */ 14 | @Configuration 15 | @ImportResource({ "classpath:webSecurityConfig.xml" }) 16 | @ComponentScan("spring.security.rest.api.security") 17 | public class SpringSecurityConfig { 18 | 19 | public SpringSecurityConfig() { 20 | super(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/SpringSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.ImportResource; 6 | 7 | /** 8 | * Expose the Spring Security Configuration 9 | * 10 | * @author malalanayake 11 | * 12 | */ 13 | @Configuration 14 | @ImportResource({ "classpath:webSecurityConfig.xml" }) 15 | @ComponentScan("spring.security.custom.rest.api.security") 16 | public class SpringSecurityConfig { 17 | 18 | public SpringSecurityConfig() { 19 | super(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/spring-security-custom-rest-api/WEB-INF/classes/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | web - %date [%thread] %-5level %logger{36} - %message%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/WebConfig.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 7 | 8 | /** 9 | * Web Configuration expose the all services 10 | * 11 | * @author malalanayake 12 | * 13 | */ 14 | @Configuration 15 | @ComponentScan("spring.security.rest.api.service") 16 | @EnableWebMvc 17 | public class WebConfig extends WebMvcConfigurerAdapter { 18 | 19 | public WebConfig() { 20 | super(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.7 13 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/WebConfig.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 7 | 8 | /** 9 | * Web Configuration expose the all services 10 | * 11 | * @author malalanayake 12 | * 13 | */ 14 | @Configuration 15 | @ComponentScan("spring.security.custom.rest.api.service") 16 | @EnableWebMvc 17 | public class WebConfig extends WebMvcConfigurerAdapter { 18 | 19 | public WebConfig() { 20 | super(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/entity/CustomerDetails.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Customer details persistence entity 7 | * 8 | * @author malalanayake 9 | * 10 | */ 11 | public class CustomerDetails implements Serializable { 12 | private long id; 13 | 14 | private String name; 15 | 16 | public CustomerDetails() { 17 | super(); 18 | } 19 | 20 | public CustomerDetails(final String name) { 21 | super(); 22 | 23 | this.name = name; 24 | } 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(final long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(final String name) { 39 | this.name = name; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/entity/CustomerDetails.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Customer details persistence entity 7 | * 8 | * @author malalanayake 9 | * 10 | */ 11 | public class CustomerDetails implements Serializable { 12 | private long id; 13 | 14 | private String name; 15 | 16 | public CustomerDetails() { 17 | super(); 18 | } 19 | 20 | public CustomerDetails(final String name) { 21 | super(); 22 | 23 | this.name = name; 24 | } 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(final long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(final String name) { 39 | this.name = name; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | spring/security/custom/rest/api/SpringSecurityConfig.class 2 | spring/security/custom/rest/api/security/RestAuthenticationAccessDeniedHandler.class 3 | spring/security/custom/rest/api/entity/CustomerDetails.class 4 | spring/security/custom/rest/api/security/CustomUserDetailsService$1.class 5 | spring/security/custom/rest/api/service/CustomerDetailService.class 6 | spring/security/custom/rest/api/security/CustomUserDetailsService$CustomRole.class 7 | spring/security/custom/rest/api/WebConfig.class 8 | spring/security/custom/rest/api/security/RestAuthenticationSuccessHandler.class 9 | spring/security/custom/rest/api/security/CustomUserDetailsService.class 10 | spring/security/custom/rest/api/security/RestAuthenticationEntryPoint.class 11 | spring/security/custom/rest/api/security/CustomUserData.class 12 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationAccessDeniedHandler.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.access.AccessDeniedException; 10 | import org.springframework.security.core.AuthenticationException; 11 | import org.springframework.security.web.access.AccessDeniedHandler; 12 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; 13 | 14 | public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler { 15 | 16 | @Override 17 | public void handle(HttpServletRequest request, HttpServletResponse response, 18 | AccessDeniedException arg2) throws IOException, ServletException { 19 | response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.core.AuthenticationException; 10 | import org.springframework.security.web.AuthenticationEntryPoint; 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * This entry point is called once the request missing their authentication. 15 | * 16 | * @author malalanayake 17 | * 18 | */ 19 | @Component 20 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 21 | 22 | @Override 23 | public void commence(HttpServletRequest arg0, HttpServletResponse arg1, 24 | AuthenticationException arg2) throws IOException, ServletException { 25 | arg1.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.core.Authentication; 10 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; 11 | 12 | /** 13 | * This will call once the request is authenticated. If it is not, the request 14 | * will be redirected to authenticate entry point 15 | * 16 | * @author malalanayake 17 | * 18 | */ 19 | public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { 20 | 21 | @Override 22 | public void onAuthenticationSuccess(final HttpServletRequest request, 23 | final HttpServletResponse response, final Authentication authentication) 24 | throws ServletException, IOException { 25 | // You can do something here for which is success requests 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/security/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.core.AuthenticationException; 10 | import org.springframework.security.web.AuthenticationEntryPoint; 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * This entry point is called once the request missing the authentication but if 15 | * the request dosn't have the cookie then we send the unauthorized response. 16 | * 17 | * @author malalanayake 18 | * 19 | */ 20 | @Component("restAuthenticationEntryPoint") 21 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 22 | 23 | @Override 24 | public void commence(HttpServletRequest arg0, HttpServletResponse arg1, 25 | AuthenticationException arg2) throws IOException, ServletException { 26 | arg1.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationSuccessHandler.java 2 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/service/CustomerDetailService.java 3 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/WebConfig.java 4 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationEntryPoint.java 5 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/RestAuthenticationAccessDeniedHandler.java 6 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/entity/CustomerDetails.java 7 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/CustomUserData.java 8 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/SpringSecurityConfig.java 9 | /Volumes/Dev/Git/spring-security/spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/CustomUserDetailsService.java 10 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/CustomUserData.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.security; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | 6 | import org.springframework.security.core.GrantedAuthority; 7 | import org.springframework.security.core.userdetails.UserDetails; 8 | 9 | /** 10 | * This class is provide the user details which is needed for authentication 11 | * 12 | * @author malalanayake 13 | * 14 | */ 15 | public class CustomUserData implements UserDetails { 16 | Collection list = null; 17 | String userName = null; 18 | String password = null; 19 | boolean status = false; 20 | 21 | public CustomUserData() { 22 | list = new ArrayList(); 23 | } 24 | 25 | @Override 26 | public Collection getAuthorities() { 27 | return this.list; 28 | } 29 | 30 | public void setAuthorities(Collection roles) { 31 | this.list = roles; 32 | } 33 | 34 | public void setAuthentication(boolean status) { 35 | this.status = status; 36 | } 37 | 38 | @Override 39 | public String getPassword() { 40 | return this.password; 41 | } 42 | 43 | public void setPassword(String pass) { 44 | this.password = pass; 45 | } 46 | 47 | @Override 48 | public String getUsername() { 49 | return this.userName; 50 | } 51 | 52 | @Override 53 | public boolean isAccountNonExpired() { 54 | return true; 55 | } 56 | 57 | @Override 58 | public boolean isAccountNonLocked() { 59 | return true; 60 | } 61 | 62 | @Override 63 | public boolean isCredentialsNonExpired() { 64 | return true; 65 | } 66 | 67 | @Override 68 | public boolean isEnabled() { 69 | return true; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | Spring MVC Application 10 | 11 | 1 12 | 13 | 14 | 15 | 16 | contextClass 17 | 18 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 19 | 20 | 21 | 22 | contextConfigLocation 23 | spring.security.rest.api 24 | 25 | 26 | 27 | org.springframework.web.context.ContextLoaderListener 28 | 29 | 30 | 31 | 32 | api 33 | org.springframework.web.servlet.DispatcherServlet 34 | 1 35 | 36 | 37 | api 38 | /api/* 39 | 40 | 41 | 42 | 43 | springSecurityFilterChain 44 | org.springframework.web.filter.DelegatingFilterProxy 45 | 46 | 47 | springSecurityFilterChain 48 | /* 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | Spring MVC Application 10 | 11 | 1 12 | 13 | 14 | 15 | 16 | contextClass 17 | 18 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 19 | 20 | 21 | 22 | contextConfigLocation 23 | spring.security.custom.rest.api 24 | 25 | 26 | 27 | org.springframework.web.context.ContextLoaderListener 28 | 29 | 30 | 31 | 32 | api 33 | org.springframework.web.servlet.DispatcherServlet 34 | 1 35 | 36 | 37 | api 38 | /api/* 39 | 40 | 41 | 42 | 43 | springSecurityFilterChain 44 | org.springframework.web.filter.DelegatingFilterProxy 45 | 46 | 47 | springSecurityFilterChain 48 | /* 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/spring-security-custom-rest-api/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | Spring MVC Application 10 | 11 | 1 12 | 13 | 14 | 15 | 16 | contextClass 17 | 18 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 19 | 20 | 21 | 22 | contextConfigLocation 23 | spring.security.custom.rest.api 24 | 25 | 26 | 27 | org.springframework.web.context.ContextLoaderListener 28 | 29 | 30 | 31 | 32 | api 33 | org.springframework.web.servlet.DispatcherServlet 34 | 1 35 | 36 | 37 | api 38 | /api/* 39 | 40 | 41 | 42 | 43 | springSecurityFilterChain 44 | org.springframework.web.filter.DelegatingFilterProxy 45 | 46 | 47 | springSecurityFilterChain 48 | /* 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/resources/webSecurityConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/service/CustomerDetailService.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.service; 2 | 3 | import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; 4 | 5 | import java.util.List; 6 | 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.context.ApplicationEventPublisher; 11 | import org.springframework.security.access.annotation.Secured; 12 | import org.springframework.stereotype.Controller; 13 | import org.springframework.web.bind.annotation.PathVariable; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | import org.springframework.web.bind.annotation.ResponseBody; 17 | import org.springframework.web.util.UriComponentsBuilder; 18 | 19 | import spring.security.custom.rest.api.entity.CustomerDetails; 20 | 21 | import com.google.common.collect.Lists; 22 | 23 | /** 24 | * Customer details exposing as a service. This is secured by spring role base 25 | * security. This service is only for ROLE_ADMIN 26 | * 27 | * @author malalanayake 28 | * 29 | */ 30 | @Controller 31 | @RequestMapping(value = "/customer") 32 | @Secured("ROLE_ADMIN") 33 | public class CustomerDetailService { 34 | 35 | @Autowired 36 | private ApplicationEventPublisher eventPublisher; 37 | 38 | public CustomerDetailService() { 39 | super(); 40 | } 41 | 42 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) 43 | @ResponseBody 44 | public CustomerDetails findById(@PathVariable("id") final Long id, 45 | final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { 46 | return new CustomerDetails(randomAlphabetic(6)); 47 | } 48 | 49 | @RequestMapping(method = RequestMethod.GET) 50 | @ResponseBody 51 | public List findAll() { 52 | return Lists.newArrayList(new CustomerDetails(randomAlphabetic(6))); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/service/CustomerDetailService.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api.service; 2 | 3 | import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; 4 | import java.util.List; 5 | import javax.servlet.http.HttpServletResponse; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.ApplicationEventPublisher; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.security.access.annotation.Secured; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | import org.springframework.web.util.UriComponentsBuilder; 16 | 17 | import spring.security.rest.api.entity.CustomerDetails; 18 | 19 | import com.google.common.collect.Lists; 20 | 21 | /** 22 | * Customer details exposing as a service. This is secured by spring role base 23 | * security. This service is only for ROLE_ADMIN 24 | * 25 | * @author malalanayake 26 | * 27 | */ 28 | @Controller 29 | @RequestMapping(value = "/customer") 30 | @Secured("ROLE_ADMIN") 31 | public class CustomerDetailService { 32 | 33 | @Autowired 34 | private ApplicationEventPublisher eventPublisher; 35 | 36 | public CustomerDetailService() { 37 | super(); 38 | } 39 | 40 | @RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = { MediaType.APPLICATION_JSON_VALUE }) 41 | @ResponseBody 42 | public CustomerDetails findById(@PathVariable("id") final Long id, 43 | final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { 44 | return new CustomerDetails(randomAlphabetic(6)); 45 | } 46 | 47 | @RequestMapping(method = RequestMethod.GET, consumes = { MediaType.APPLICATION_JSON_VALUE }) 48 | @ResponseBody 49 | public List findAll() { 50 | return Lists.newArrayList(new CustomerDetails(randomAlphabetic(6))); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/classes/webSecurityConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 27 | 28 | 29 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/resources/webSecurityConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 27 | 28 | 29 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/target/spring-security-custom-rest-api/WEB-INF/classes/webSecurityConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 27 | 28 | 29 | 31 | 32 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring.security.rest.api/src/main/java/spring/security/rest/api/security/RestAuthenticationSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package spring.security.rest.api.security; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.core.Authentication; 10 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; 11 | import org.springframework.security.web.savedrequest.HttpSessionRequestCache; 12 | import org.springframework.security.web.savedrequest.RequestCache; 13 | import org.springframework.security.web.savedrequest.SavedRequest; 14 | import org.springframework.util.StringUtils; 15 | 16 | /** 17 | * This will call once the request is authenticated. If it is not, the request 18 | * will be redirected to authenticate entry point 19 | * 20 | * @author malalanayake 21 | * 22 | */ 23 | public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { 24 | private RequestCache requestCache = new HttpSessionRequestCache(); 25 | 26 | @Override 27 | public void onAuthenticationSuccess(final HttpServletRequest request, 28 | final HttpServletResponse response, final Authentication authentication) 29 | throws ServletException, IOException { 30 | final SavedRequest savedRequest = requestCache.getRequest(request, response); 31 | 32 | if (savedRequest == null) { 33 | clearAuthenticationAttributes(request); 34 | return; 35 | } 36 | final String targetUrlParameter = getTargetUrlParameter(); 37 | if (isAlwaysUseDefaultTargetUrl() 38 | || (targetUrlParameter != null && StringUtils.hasText(request 39 | .getParameter(targetUrlParameter)))) { 40 | requestCache.removeRequest(request, response); 41 | clearAuthenticationAttributes(request); 42 | return; 43 | } 44 | 45 | clearAuthenticationAttributes(request); 46 | 47 | // Use the DefaultSavedRequest URL 48 | // final String targetUrl = savedRequest.getRedirectUrl(); 49 | // logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl); 50 | // getRedirectStrategy().sendRedirect(request, response, targetUrl); 51 | } 52 | 53 | public void setRequestCache(final RequestCache requestCache) { 54 | this.requestCache = requestCache; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/src/main/java/spring/security/custom/rest/api/security/CustomUserDetailsService.java: -------------------------------------------------------------------------------- 1 | package spring.security.custom.rest.api.security; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | 6 | import org.springframework.security.core.GrantedAuthority; 7 | import org.springframework.security.core.userdetails.UserDetails; 8 | import org.springframework.security.core.userdetails.UserDetailsService; 9 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 10 | 11 | /** 12 | * CustomUserDetailsService provides the connection point to external data 13 | * source 14 | * 15 | * @author malalanayake 16 | * 17 | */ 18 | public class CustomUserDetailsService implements UserDetailsService { 19 | private String USER_ADMIN = "admin"; 20 | private String PASS_ADMIN = "adminpass"; 21 | 22 | private String USER = "user"; 23 | private String PASS = "userpass"; 24 | 25 | @Override 26 | public UserDetails loadUserByUsername(String authentication) throws UsernameNotFoundException { 27 | CustomUserData customUserData = new CustomUserData(); 28 | // You can talk to any of your user details service and get the 29 | // authentication data and return as CustomUserData object then spring 30 | // framework will take care of the authentication 31 | if (USER_ADMIN.equals(authentication)) { 32 | customUserData.setAuthentication(true); 33 | customUserData.setPassword(PASS_ADMIN); 34 | Collection roles = new ArrayList(); 35 | CustomRole customRole = new CustomRole(); 36 | customRole.setAuthority("ROLE_ADMIN"); 37 | roles.add(customRole); 38 | customUserData.setAuthorities(roles); 39 | return customUserData; 40 | } else if (USER.equals(authentication)) { 41 | customUserData.setAuthentication(true); 42 | customUserData.setPassword(PASS); 43 | Collection roles = new ArrayList(); 44 | CustomRole customRole = new CustomRole(); 45 | customRole.setAuthority("ROLE_USER"); 46 | roles.add(customRole); 47 | customUserData.setAuthorities(roles); 48 | return customUserData; 49 | } else { 50 | return null; 51 | } 52 | } 53 | 54 | /** 55 | * Custom Role class for manage the authorities 56 | * 57 | * @author malalanayake 58 | * 59 | */ 60 | private class CustomRole implements GrantedAuthority { 61 | String role = null; 62 | 63 | @Override 64 | public String getAuthority() { 65 | return role; 66 | } 67 | 68 | public void setAuthority(String roleName) { 69 | this.role = roleName; 70 | } 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring.security.rest.api/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | spring 6 | spring.security.rest.api 7 | 0.0.1-SNAPSHOT 8 | 9 | spring.security.rest.api 10 | war 11 | 12 | 13 | 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 4.0.5.RELEASE 19 | 3.2.4.RELEASE 20 | 21 | 22 | 4.3.5.Final 23 | 5.1.30 24 | 25 | 26 | 1.7.6 27 | 1.1.1 28 | 29 | 30 | 5.1.1.Final 31 | 32 | 33 | 17.0 34 | 3.3.2 35 | 36 | 37 | 1.3 38 | 4.11 39 | 1.9.5 40 | 41 | 4.3.2 42 | 4.3.3 43 | 44 | 2.3.1 45 | 46 | 47 | 3.1 48 | 2.4 49 | 2.17 50 | 1.4.8 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.security 59 | spring-security-web 60 | ${org.springframework.security.version} 61 | 62 | 63 | org.springframework.security 64 | spring-security-config 65 | ${org.springframework.security.version} 66 | 67 | 68 | 69 | 70 | 71 | org.springframework 72 | spring-core 73 | ${org.springframework.version} 74 | 75 | 76 | commons-logging 77 | commons-logging 78 | 79 | 80 | 81 | 82 | org.springframework 83 | spring-context 84 | ${org.springframework.version} 85 | 86 | 87 | org.springframework 88 | spring-jdbc 89 | ${org.springframework.version} 90 | 91 | 92 | org.springframework 93 | spring-beans 94 | ${org.springframework.version} 95 | 96 | 97 | org.springframework 98 | spring-aop 99 | ${org.springframework.version} 100 | 101 | 102 | org.springframework 103 | spring-tx 104 | ${org.springframework.version} 105 | 106 | 107 | org.springframework 108 | spring-expression 109 | ${org.springframework.version} 110 | 111 | 112 | 113 | org.springframework 114 | spring-web 115 | ${org.springframework.version} 116 | 117 | 118 | org.springframework 119 | spring-webmvc 120 | ${org.springframework.version} 121 | 122 | 123 | 124 | 125 | 126 | javax.servlet 127 | javax.servlet-api 128 | 3.0.1 129 | provided 130 | 131 | 132 | 133 | javax.servlet 134 | jstl 135 | 1.2 136 | runtime 137 | 138 | 139 | 140 | 141 | 142 | com.fasterxml.jackson.core 143 | jackson-databind 144 | 2.2.2 145 | 146 | 147 | 148 | 149 | 150 | com.google.guava 151 | guava 152 | ${guava.version} 153 | 154 | 155 | org.apache.commons 156 | commons-lang3 157 | 3.1 158 | 159 | 160 | 161 | 162 | org.slf4j 163 | slf4j-api 164 | ${org.slf4j.version} 165 | 166 | 167 | ch.qos.logback 168 | logback-classic 169 | ${logback.version} 170 | 171 | 172 | 173 | org.slf4j 174 | jcl-over-slf4j 175 | ${org.slf4j.version} 176 | 177 | 178 | 179 | org.slf4j 180 | log4j-over-slf4j 181 | ${org.slf4j.version} 182 | 183 | 184 | 185 | 186 | 194 | 195 | 196 | 197 | 198 | spring-security-rest-api 199 | 200 | 201 | src/main/resources 202 | true 203 | 204 | 205 | 206 | 207 | 208 | 209 | org.apache.maven.plugins 210 | maven-compiler-plugin 211 | 3.1 212 | 213 | 1.7 214 | 1.7 215 | 216 | 217 | 218 | 219 | org.apache.maven.plugins 220 | maven-war-plugin 221 | ${maven-war-plugin.version} 222 | 223 | 224 | 225 | org.apache.maven.plugins 226 | maven-surefire-plugin 227 | ${maven-surefire-plugin.version} 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | org.codehaus.cargo 240 | cargo-maven2-plugin 241 | ${cargo-maven2-plugin.version} 242 | 243 | true 244 | 245 | jetty8x 246 | embedded 247 | 248 | 249 | 250 | 251 | 252 | 253 | 8082 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /spring.security.custom.rest.api/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | spring 6 | spring.security.custom.rest.api 7 | 0.0.1-SNAPSHOT 8 | 9 | spring.security.custom.rest.api 10 | war 11 | 12 | 13 | 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 4.0.5.RELEASE 19 | 3.2.4.RELEASE 20 | 21 | 22 | 4.3.5.Final 23 | 5.1.30 24 | 25 | 26 | 1.7.6 27 | 1.1.1 28 | 29 | 30 | 5.1.1.Final 31 | 32 | 33 | 17.0 34 | 3.3.2 35 | 36 | 37 | 1.3 38 | 4.11 39 | 1.9.5 40 | 41 | 4.3.2 42 | 4.3.3 43 | 44 | 2.3.1 45 | 46 | 47 | 3.1 48 | 2.4 49 | 2.17 50 | 1.4.8 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.security 59 | spring-security-web 60 | ${org.springframework.security.version} 61 | 62 | 63 | org.springframework.security 64 | spring-security-config 65 | ${org.springframework.security.version} 66 | 67 | 68 | 69 | 70 | 71 | org.springframework 72 | spring-core 73 | ${org.springframework.version} 74 | 75 | 76 | commons-logging 77 | commons-logging 78 | 79 | 80 | 81 | 82 | org.springframework 83 | spring-context 84 | ${org.springframework.version} 85 | 86 | 87 | org.springframework 88 | spring-jdbc 89 | ${org.springframework.version} 90 | 91 | 92 | org.springframework 93 | spring-beans 94 | ${org.springframework.version} 95 | 96 | 97 | org.springframework 98 | spring-aop 99 | ${org.springframework.version} 100 | 101 | 102 | org.springframework 103 | spring-tx 104 | ${org.springframework.version} 105 | 106 | 107 | org.springframework 108 | spring-expression 109 | ${org.springframework.version} 110 | 111 | 112 | 113 | org.springframework 114 | spring-web 115 | ${org.springframework.version} 116 | 117 | 118 | org.springframework 119 | spring-webmvc 120 | ${org.springframework.version} 121 | 122 | 123 | 124 | 125 | 126 | javax.servlet 127 | javax.servlet-api 128 | 3.0.1 129 | provided 130 | 131 | 132 | 133 | javax.servlet 134 | jstl 135 | 1.2 136 | runtime 137 | 138 | 139 | 140 | 141 | 142 | com.fasterxml.jackson.core 143 | jackson-databind 144 | 2.2.2 145 | 146 | 147 | 148 | 149 | 150 | com.google.guava 151 | guava 152 | ${guava.version} 153 | 154 | 155 | org.apache.commons 156 | commons-lang3 157 | 3.1 158 | 159 | 160 | 161 | 162 | org.slf4j 163 | slf4j-api 164 | ${org.slf4j.version} 165 | 166 | 167 | ch.qos.logback 168 | logback-classic 169 | ${logback.version} 170 | 171 | 172 | 173 | org.slf4j 174 | jcl-over-slf4j 175 | ${org.slf4j.version} 176 | 177 | 178 | 179 | org.slf4j 180 | log4j-over-slf4j 181 | ${org.slf4j.version} 182 | 183 | 184 | 185 | 186 | 194 | 195 | 196 | 197 | 198 | spring-security-custom-rest-api 199 | 200 | 201 | src/main/resources 202 | true 203 | 204 | 205 | 206 | 207 | 208 | 209 | org.apache.maven.plugins 210 | maven-compiler-plugin 211 | 3.1 212 | 213 | 1.7 214 | 1.7 215 | 216 | 217 | 218 | 219 | org.apache.maven.plugins 220 | maven-war-plugin 221 | ${maven-war-plugin.version} 222 | 223 | 224 | 225 | org.apache.maven.plugins 226 | maven-surefire-plugin 227 | ${maven-surefire-plugin.version} 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | org.codehaus.cargo 240 | cargo-maven2-plugin 241 | ${cargo-maven2-plugin.version} 242 | 243 | true 244 | 245 | jetty8x 246 | embedded 247 | 248 | 249 | 250 | 251 | 252 | 253 | 8082 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | --------------------------------------------------------------------------------