diff --git a/jee7/pom.xml b/jee7/pom.xml index e633d2df3d..308f36c7a2 100644 --- a/jee7/pom.xml +++ b/jee7/pom.xml @@ -31,6 +31,7 @@ 1.0.0.Final 2.6 + 4.2.2.RELEASE @@ -136,6 +137,34 @@ standard 1.1.2 + + + javax.mvc + javax.mvc-api + 20160715 + + + org.glassfish.ozark + ozark + 20160715 + + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + org.springframework.security + spring-security-taglibs + ${org.springframework.security.version} + @@ -378,4 +407,18 @@ + + + + bintray-mvc-spec-maven + bintray + http://dl.bintray.com/mvc-spec/maven + + true + + + false + + + diff --git a/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java b/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java new file mode 100755 index 0000000000..f8e7982253 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java @@ -0,0 +1,13 @@ +package com.baeldung.springSecurity; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +/** + * Application class required by JAX-RS. If you don't want to have any + * prefix in the URL, you can set the application path to "/". + */ +@ApplicationPath("/") +public class ApplicationConfig extends Application { + +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java b/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java new file mode 100644 index 0000000000..e6a05f7b71 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java @@ -0,0 +1,10 @@ +package com.baeldung.springSecurity; + +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; + +public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { + + public SecurityWebApplicationInitializer() { + super(SpringSecurityConfig.class); + } +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java b/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java new file mode 100644 index 0000000000..bda8930f36 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java @@ -0,0 +1,46 @@ +package com.baeldung.springSecurity; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user1") + .password("user1Pass") + .roles("USER") + .and() + .withUser("admin") + .password("adminPass") + .roles("ADMIN"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .csrf() + .disable() + .authorizeRequests() + .antMatchers("/auth/login*") + .anonymous() + .antMatchers("/home/admin*") + .hasRole("ADMIN") + .anyRequest() + .authenticated() + .and() + .formLogin() + .loginPage("/auth/login") + .defaultSuccessUrl("/home", true) + .failureUrl("/auth/login?error=true") + .and() + .logout() + .logoutSuccessUrl("/auth/login"); + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java b/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java new file mode 100644 index 0000000000..53fd9f4b81 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java @@ -0,0 +1,28 @@ +package com.baeldung.springSecurity.controller; + +import javax.mvc.annotation.Controller; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/home") +@Controller +public class HomeController { + + @GET + public String home() { + return "home.jsp"; + } + + @GET + @Path("/user") + public String admin() { + return "user.jsp"; + } + + @GET + @Path("/admin") + public String user() { + return "admin.jsp"; + } + +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java b/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java new file mode 100644 index 0000000000..a7e7bb471d --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java @@ -0,0 +1,15 @@ +package com.baeldung.springSecurity.controller; + +import javax.mvc.annotation.Controller; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/auth/login") +@Controller +public class LoginController { + + @GET + public String login() { + return "login.jsp"; + } +} diff --git a/jee7/src/main/webapp/WEB-INF/views/admin.jsp b/jee7/src/main/webapp/WEB-INF/views/admin.jsp new file mode 100644 index 0000000000..b83ea09f5b --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/admin.jsp @@ -0,0 +1,12 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

Welcome to the ADMIN page

+ + ">Logout + + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/home.jsp b/jee7/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 0000000000..c6e129c9ce --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,26 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

This is the body of the sample view

+ + + This text is only visible to a user +

+ ">Restricted Admin Page +

+
+ + + This text is only visible to an admin +
+ ">Admin Page +
+
+ + ">Logout + + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/login.jsp b/jee7/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 0000000000..d6f2e56f3a --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,26 @@ + + + + +

Login

+ +
+ + + + + + + + + + + + + +
User:
Password:
+ +
+ + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/user.jsp b/jee7/src/main/webapp/WEB-INF/views/user.jsp new file mode 100644 index 0000000000..11b8155da7 --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/user.jsp @@ -0,0 +1,12 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

Welcome to the Restricted Admin page

+ + ">Logout + + + \ No newline at end of file