Skip to content

Commit 2476059

Browse files
lor6KevinGilmore
authored andcommitted
2 login pages config (eugenp#1081)
* 2 login pages config * fix links * small fix * fix formatting
1 parent 47889bc commit 2476059

10 files changed

Lines changed: 316 additions & 0 deletions

File tree

‎spring-mvc-forms/pom.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646
<artifactId>commons-fileupload</artifactId>
4747
<version>${fileupload.version}</version>
4848
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.security</groupId>
52+
<artifactId>spring-security-web</artifactId>
53+
<version>${org.springframework.security.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.security</groupId>
57+
<artifactId>spring-security-config</artifactId>
58+
<version>${org.springframework.security.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.security</groupId>
62+
<artifactId>spring-security-taglibs</artifactId>
63+
<version>${org.springframework.security.version}</version>
64+
</dependency>
65+
4966
</dependencies>
5067
<profiles>
5168
<!-- Local -->
@@ -98,6 +115,7 @@
98115
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
99116
<deploy-path>enter-location-of-server</deploy-path>
100117
<fileupload.version>1.3.2</fileupload.version>
118+
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
101119
</properties>
102120

103121
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.baeldung.springmvcforms.configuration;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.core.annotation.Order;
7+
import org.springframework.security.authentication.AuthenticationProvider;
8+
import org.springframework.security.authentication.TestingAuthenticationProvider;
9+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13+
import org.springframework.security.core.userdetails.User;
14+
import org.springframework.security.core.userdetails.UserDetailsService;
15+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
16+
17+
@Configuration
18+
@EnableWebSecurity
19+
public class SecurityConfig {
20+
21+
@Bean
22+
public UserDetailsService userDetailsService() throws Exception {
23+
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
24+
manager.createUser(User.withUsername("user")
25+
.password("userPass")
26+
.roles("USER")
27+
.build());
28+
manager.createUser(User.withUsername("admin")
29+
.password("adminPass")
30+
.roles("ADMIN")
31+
.build());
32+
return manager;
33+
}
34+
35+
@Configuration
36+
@Order(1)
37+
public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter {
38+
39+
public App1ConfigurationAdapter() {
40+
super();
41+
}
42+
43+
@Override
44+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
45+
auth.inMemoryAuthentication()
46+
.withUser("admin")
47+
.password("admin")
48+
.roles("ADMIN");
49+
}
50+
51+
@Override
52+
protected void configure(HttpSecurity http) throws Exception {
53+
http.antMatcher("/admin*")
54+
.authorizeRequests()
55+
.anyRequest()
56+
.hasRole("ADMIN")
57+
// log in
58+
.and()
59+
.formLogin()
60+
.loginPage("/loginAdmin")
61+
.loginProcessingUrl("/admin_login")
62+
.failureUrl("/loginAdmin?error=loginError")
63+
.defaultSuccessUrl("/adminPage")
64+
// logout
65+
.and()
66+
.logout()
67+
.logoutUrl("/admin_logout")
68+
.logoutSuccessUrl("/protectedLinks")
69+
.deleteCookies("JSESSIONID")
70+
.and()
71+
.exceptionHandling()
72+
.accessDeniedPage("/403")
73+
.and()
74+
.csrf()
75+
.disable();
76+
}
77+
}
78+
79+
@Configuration
80+
@Order(2)
81+
public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {
82+
83+
public App2ConfigurationAdapter() {
84+
super();
85+
}
86+
87+
@Override
88+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
89+
auth.inMemoryAuthentication()
90+
.withUser("user")
91+
.password("user")
92+
.roles("USER");
93+
}
94+
95+
protected void configure(HttpSecurity http) throws Exception {
96+
http.antMatcher("/user*")
97+
.authorizeRequests()
98+
.anyRequest()
99+
.hasRole("USER")
100+
// log in
101+
.and()
102+
.formLogin()
103+
.loginPage("/loginUser")
104+
.loginProcessingUrl("/user_login")
105+
.failureUrl("/loginUser?error=loginError")
106+
.defaultSuccessUrl("/userPage")
107+
// logout
108+
.and()
109+
.logout()
110+
.logoutUrl("/user_logout")
111+
.logoutSuccessUrl("/protectedLinks")
112+
.deleteCookies("JSESSIONID")
113+
.and()
114+
.exceptionHandling()
115+
.accessDeniedPage("/403")
116+
.and()
117+
.csrf()
118+
.disable();
119+
}
120+
}
121+
122+
}

‎spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.web.WebApplicationInitializer;
44
import org.springframework.web.context.ContextLoaderListener;
55
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
import org.springframework.web.filter.DelegatingFilterProxy;
67
import org.springframework.web.servlet.DispatcherServlet;
78

89
import javax.servlet.ServletContext;
@@ -24,6 +25,9 @@ public void onStartup(ServletContext container) throws ServletException {
2425

2526
servlet.setLoadOnStartup(1);
2627
servlet.addMapping("/");
28+
29+
container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
30+
.addMappingForUrlPatterns(null, false, "/*");
2731
}
2832
// @Override
2933
// public void onStartup(ServletContext container) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.springmvcforms.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
public class UsersController {
8+
9+
@RequestMapping("/protectedLinks")
10+
public String getAnonymousPage() {
11+
return "protectedLinks";
12+
}
13+
14+
@RequestMapping("/userPage")
15+
public String getUserPage() {
16+
return "userPage";
17+
}
18+
19+
@RequestMapping("/adminPage")
20+
public String getAdminPage() {
21+
return "adminPage";
22+
}
23+
24+
@RequestMapping("/loginAdmin")
25+
public String getAdminLoginPage() {
26+
return "loginAdmin";
27+
}
28+
29+
@RequestMapping("/loginUser")
30+
public String getUserLoginPage() {
31+
return "loginUser";
32+
}
33+
34+
@RequestMapping("/403")
35+
public String getAccessDeniedPage() {
36+
return "403";
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title></title>
8+
</head>
9+
<body>
10+
Your do not have permission to view this page.
11+
</body>
12+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Insert title here</title>
9+
</head>
10+
<body>
11+
Welcome admin! <a href="<c:url value='/admin_logout' /> " >Logout</a>
12+
13+
<br /><br />
14+
<a href="<c:url value='/protectedLinks' /> " >Back to links</a>
15+
</body>
16+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Insert title here</title>
9+
</head>
10+
<body>
11+
12+
<p>Admin login page</p>
13+
<form name="f" action="admin_login" method="POST">
14+
15+
<table>
16+
<tr>
17+
<td>User:</td>
18+
<td><input type="text" name="username" value=""></td>
19+
</tr>
20+
<tr>
21+
<td>Password:</td>
22+
<td><input type="password" name="password" /></td>
23+
</tr>
24+
<tr>
25+
<td><input name="submit" type="submit" value="submit" /></td>
26+
</tr>
27+
</table>
28+
29+
</form>
30+
31+
<%
32+
if (request.getParameter("error") != null) {
33+
out.println("Login failed!");
34+
}
35+
%>
36+
37+
</body>
38+
</html>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Login</title>
9+
</head>
10+
<body>
11+
12+
<p>User login page</p>
13+
14+
<form name="f" action="user_login" method="POST">
15+
<table>
16+
<tr>
17+
<td>User:</td>
18+
<td><input type="text" name="username" value=""></td>
19+
</tr>
20+
<tr>
21+
<td>Password:</td>
22+
<td><input type="password" name="password" /></td>
23+
</tr>
24+
<tr>
25+
<td><input name="submit" type="submit" value="submit" /></td>
26+
</tr>
27+
</table>
28+
29+
</form>
30+
<%
31+
if (request.getParameter("error") != null) {
32+
out.println("Login failed!");
33+
}
34+
%>
35+
36+
</body>
37+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Insert title here</title>
9+
</head>
10+
<body>
11+
12+
<a href="<c:url value="/userPage" />">User page</a>
13+
<br />
14+
<a href="<c:url value="/adminPage" />">Admin page</a>
15+
</body>
16+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Insert title here</title>
9+
</head>
10+
<body>
11+
Welcome user! <a href="<c:url value='/user_logout' /> " >Logout</a>
12+
<br /><br />
13+
<a href="<c:url value='/protectedLinks' /> " >Back to links</a>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)