Skip to content

Commit 21aa127

Browse files
lor6Eugen
authored andcommitted
add entry points (eugenp#1413)
1 parent f6e570c commit 21aa127

7 files changed

Lines changed: 118 additions & 12 deletions

File tree

‎spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/MultipleEntryPointsSecurityConfig.java‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import org.springframework.security.core.userdetails.User;
1010
import org.springframework.security.core.userdetails.UserDetailsService;
1111
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
12+
import org.springframework.security.web.AuthenticationEntryPoint;
13+
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
14+
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
15+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
1216

1317
@Configuration
1418
@EnableWebSecurity
@@ -31,28 +35,49 @@ protected void configure(HttpSecurity http) throws Exception {
3135
//@formatter:off
3236
http.antMatcher("/admin/**")
3337
.authorizeRequests().anyRequest().hasRole("ADMIN")
34-
.and().httpBasic()
38+
.and().httpBasic().authenticationEntryPoint(authenticationEntryPoint())
3539
.and().exceptionHandling().accessDeniedPage("/403");
3640
//@formatter:on
3741
}
42+
43+
@Bean
44+
public AuthenticationEntryPoint authenticationEntryPoint(){
45+
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
46+
entryPoint.setRealmName("admin realm");
47+
return entryPoint;
48+
}
3849
}
3950

4051
@Configuration
4152
@Order(2)
4253
public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {
4354

4455
protected void configure(HttpSecurity http) throws Exception {
56+
4557
//@formatter:off
4658
http.antMatcher("/user/**")
4759
.authorizeRequests().anyRequest().hasRole("USER")
48-
.and().formLogin().loginPage("/userLogin").loginProcessingUrl("/user/login")
60+
.and().formLogin().loginProcessingUrl("/user/login")
4961
.failureUrl("/userLogin?error=loginError").defaultSuccessUrl("/user/myUserPage")
5062
.and().logout().logoutUrl("/user/logout").logoutSuccessUrl("/multipleHttpLinks")
5163
.deleteCookies("JSESSIONID")
52-
.and().exceptionHandling().accessDeniedPage("/403")
64+
.and().exceptionHandling()
65+
.defaultAuthenticationEntryPointFor(loginUrlauthenticationEntryPointWithWarning(), new AntPathRequestMatcher("/user/private/**"))
66+
.defaultAuthenticationEntryPointFor(loginUrlauthenticationEntryPoint(), new AntPathRequestMatcher("/user/general/**"))
67+
.accessDeniedPage("/403")
5368
.and().csrf().disable();
5469
//@formatter:on
5570
}
71+
72+
@Bean
73+
public AuthenticationEntryPoint loginUrlauthenticationEntryPoint(){
74+
return new LoginUrlAuthenticationEntryPoint("/userLogin");
75+
}
76+
77+
@Bean
78+
public AuthenticationEntryPoint loginUrlauthenticationEntryPointWithWarning(){
79+
return new LoginUrlAuthenticationEntryPoint("/userLoginWithWarning");
80+
}
5681
}
5782

5883
@Configuration

‎spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/PagesController.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ public String getAdminPage() {
1616
return "multipleHttpElems/myAdminPage";
1717
}
1818

19-
@RequestMapping("/user/myUserPage")
19+
@RequestMapping("/user/general/myUserPage")
2020
public String getUserPage() {
2121
return "multipleHttpElems/myUserPage";
2222
}
23+
24+
@RequestMapping("/user/private/myPrivateUserPage")
25+
public String getPrivateUserPage() {
26+
return "multipleHttpElems/myPrivateUserPage";
27+
}
2328

2429
@RequestMapping("/guest/myGuestPage")
2530
public String getGuestPage() {
@@ -30,6 +35,11 @@ public String getGuestPage() {
3035
public String getUserLoginPage() {
3136
return "multipleHttpElems/login";
3237
}
38+
39+
@RequestMapping("/userLoginWithWarning")
40+
public String getUserLoginPageWithWarning() {
41+
return "multipleHttpElems/loginWithWarning";
42+
}
3343

3444
@RequestMapping("/403")
3545
public String getAccessDeniedPage() {

‎spring-security-mvc-boot/src/main/resources/spring-security-multiple-entry.xml‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:security="http://www.springframework.org/schema/security"
5-
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd
5+
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

88
<security:authentication-manager>
@@ -14,24 +14,52 @@
1414
</security:authentication-provider>
1515
</security:authentication-manager>
1616

17-
<security:http pattern="/user/**" use-expressions="true" auto-config="true">
18-
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
19-
<security:form-login login-page="/userLogin" login-processing-url="/user/login"
17+
<security:http pattern="/user/general/**" use-expressions="true" auto-config="true"
18+
entry-point-ref="loginUrlAuthenticationEntryPoint">
19+
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
20+
<security:form-login login-processing-url="/user/general/login"
2021
authentication-failure-url="/userLogin?error=loginError"
2122
default-target-url="/user/myUserPage"/>
2223
<security:csrf disabled="true"/>
2324
<security:access-denied-handler error-page="/403"/>
2425
<security:logout logout-url="/user/logout" delete-cookies="JSESSIONID" logout-success-url="/multipleHttpLinks"/>
2526
</security:http>
2627

28+
<bean id="loginUrlAuthenticationEntryPoint"
29+
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
30+
<constructor-arg name="loginFormUrl" value="/userLogin" />
31+
</bean>
32+
33+
<security:http pattern="/user/private/**" use-expressions="true" auto-config="true"
34+
entry-point-ref="loginUrlAuthenticationEntryPointWithWarning">
35+
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
36+
<security:form-login login-processing-url="/user/private/login"
37+
authentication-failure-url="/userLogin?error=loginError"
38+
default-target-url="/user/myUserPage" />
39+
<security:csrf disabled="true"/>
40+
<security:access-denied-handler error-page="/403"/>
41+
<security:logout logout-url="/user/logout" delete-cookies="JSESSIONID" logout-success-url="/multipleHttpLinks"/>
42+
</security:http>
43+
44+
<bean id="loginUrlAuthenticationEntryPointWithWarning"
45+
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
46+
<constructor-arg name="loginFormUrl" value="/userLoginWithWarning" />
47+
</bean>
48+
2749
<security:http pattern="/admin/**" use-expressions="true" auto-config="true">
2850
<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
29-
<security:http-basic/>
51+
<security:http-basic entry-point-ref="authenticationEntryPoint" />
3052
<security:access-denied-handler error-page="/403"/>
3153
</security:http>
54+
55+
<bean id="authenticationEntryPoint"
56+
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
57+
<property name="realmName" value="admin realm" />
58+
</bean>
3259

3360
<security:http pattern="/**" use-expressions="true" auto-config="true">
3461
<security:intercept-url pattern="/guest/**" access="permitAll()"/>
3562
</security:http>
3663

64+
3765
</beans>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head></head>
3+
4+
<body>
5+
<h1>Login</h1>
6+
<h3>Warning! You are about to access sensible data!</h3>
7+
8+
<form name='f' action="user/login" method='POST'>
9+
10+
<table>
11+
<tr>
12+
<td>Username:</td>
13+
<td><input type="text" name="username" /></td>
14+
</tr>
15+
<tr>
16+
<td>Password:</td>
17+
<td><input type="password" name="password" /></td>
18+
</tr>
19+
20+
<tr>
21+
<td><input name="submit" type="submit" value="submit" /></td>
22+
</tr>
23+
</table>
24+
25+
</form>
26+
27+
</body>
28+
</html>

‎spring-security-mvc-boot/src/main/resources/templates/multipleHttpElems/multipleHttpLinks.html‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
<a th:href="@{/admin/myAdminPage}">Admin page</a>
1010
<br />
11-
<a th:href="@{/user/myUserPage}">User page</a>
11+
<a th:href="@{/user/general/myUserPage}">User page</a>
12+
<br />
13+
<a th:href="@{/user/private/myPrivateUserPage}">Private user page</a>
1214
<br />
1315
<a th:href="@{/guest/myGuestPage}">Guest page</a>
1416

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="ISO-8859-1" />
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
Welcome user to your private page! <a th:href="@{/user/logout}" >Logout</a>
9+
10+
<br /><br />
11+
<a th:href="@{/multipleHttpLinks}" >Back to links</a>
12+
</body>
13+
</html>

‎spring-security-mvc-boot/src/test/java/org/baeldung/web/MultipleEntryPointsTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public void whenTestAdminCredentials_thenOk() throws Exception {
4646

4747
@Test
4848
public void whenTestUserCredentials_thenOk() throws Exception {
49-
mockMvc.perform(get("/user/myUserPage")).andExpect(status().isFound());
49+
mockMvc.perform(get("/user/general/myUserPage")).andExpect(status().isFound());
5050

51-
mockMvc.perform(get("/user/myUserPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isOk());
51+
mockMvc.perform(get("/user/general/myUserPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isOk());
5252

5353
mockMvc.perform(get("/admin/myAdminPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isForbidden());
5454
}

0 commit comments

Comments
 (0)