Skip to content

Commit be05fab

Browse files
nnhai1991jzheaux
authored andcommitted
Spring Security Taglibs (eugenp#4947)
* BAEL-1846: Java Image to Base64 String * Move from using main method to Junit test * Update to use environment variables for testing * reformat and add test file * spring boot jsp security taglibs * add more test * add more test * refactor spring config * refactor spring config * Update README.md * fi alignment * fix requested comments * additional tests and content * additional tests and content * update examples * Delete Readme file * edit form example * adding example for spring boot security tag libs * Remove old tag libs module
1 parent aa2822c commit be05fab

8 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎spring-boot-security/pom.xml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-starter-web</artifactId>
4646
</dependency>
47+
48+
<!-- security taglib -->
49+
<dependency>
50+
<groupId>org.springframework.security</groupId>
51+
<artifactId>spring-security-taglibs</artifactId>
52+
</dependency>
53+
54+
<!-- JSTL -->
55+
<dependency>
56+
<groupId>org.apache.tomcat.embed</groupId>
57+
<artifactId>tomcat-embed-jasper</artifactId>
58+
<scope>provided</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>javax.servlet</groupId>
62+
<artifactId>jstl</artifactId>
63+
</dependency>
4764

4865
<dependency>
4966
<groupId>org.springframework.boot</groupId>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.springsecuritytaglibs;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
@RequestMapping("/")
8+
public class HomeController {
9+
10+
@RequestMapping
11+
public String home() {
12+
return "home";
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.springsecuritytaglibs;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.context.annotation.PropertySource;
5+
6+
@SpringBootApplication
7+
@PropertySource("classpath:application-taglibs.properties")
8+
public class SpringBootSecurityTagLibsApplication {
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.springsecuritytaglibs.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8+
9+
@Configuration
10+
@EnableWebSecurity
11+
public class SpringBootSecurityTagLibsConfig extends WebSecurityConfigurerAdapter {
12+
13+
@Override
14+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
15+
auth.inMemoryAuthentication()
16+
.withUser("testUser")
17+
.password("password")
18+
.roles("ADMIN");
19+
}
20+
21+
@Override
22+
protected void configure(HttpSecurity http) throws Exception {
23+
// @formatter:off
24+
http.csrf()
25+
.and()
26+
.authorizeRequests()
27+
.antMatchers("/userManagement").hasRole("ADMIN")
28+
.anyRequest().permitAll().and().httpBasic();
29+
// @formatter:on
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#jsp config
2+
spring.mvc.view.prefix: /WEB-INF/views/
3+
spring.mvc.view.suffix: .jsp
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
22
#security.user.password=password
33
#security.oauth2.client.client-id=client
4-
#security.oauth2.client.client-secret=secret
4+
#security.oauth2.client.client-secret=secret
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=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%@ taglib prefix="sec"
4+
uri="http://www.springframework.org/security/tags"%>
5+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6+
<html>
7+
<head>
8+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
9+
<sec:csrfMetaTags />
10+
<title>Home Page</title>
11+
</head>
12+
<body>
13+
<sec:authorize access="!isAuthenticated()">
14+
Login
15+
</sec:authorize>
16+
17+
<sec:authorize access="isAuthenticated()">
18+
Logout
19+
</sec:authorize>
20+
21+
<sec:authorize access="isAuthenticated()">
22+
<h2>
23+
Welcome back, <sec:authentication property="name" />
24+
</h2>
25+
<sec:authorize access="hasRole('ADMIN')">
26+
Manage Users
27+
</sec:authorize>
28+
<form method="post">
29+
<sec:csrfInput />
30+
Text Field: <br /> <input type="text" name="textField" />
31+
<input type="submit" value="Submit form with CSRF input">
32+
</form>
33+
<sec:authorize url="/userManagement">
34+
<a href="/userManagement">Manage Users</a>
35+
</sec:authorize>
36+
</sec:authorize>
37+
</body>
38+
</html>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.springsecuritytaglibs;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = SpringBootSecurityTagLibsApplication.class)
16+
public class HomeControllerUnitTest {
17+
18+
@Autowired
19+
private TestRestTemplate restTemplate;
20+
21+
@Test
22+
public void whenUserIsAuthenticatedThenAuthenticatedSectionsShowOnSite() throws Exception {
23+
String body = this.restTemplate.withBasicAuth("testUser", "password")
24+
.getForEntity("/", String.class)
25+
.getBody();
26+
27+
// test <sec:authorize access="!isAuthenticated()">
28+
assertFalse(body.contains("Login"));
29+
30+
// test <sec:authorize access="isAuthenticated()">
31+
assertTrue(body.contains("Logout"));
32+
33+
// test <sec:authorize access="hasRole('ADMIN')">
34+
assertTrue(body.contains("Manage Users"));
35+
36+
// test <sec:authentication property="principal.username" />
37+
assertTrue(body.contains("testUser"));
38+
39+
// test <sec:authorize url="/adminOnlyURL">
40+
assertTrue(body.contains("<a href=\"/userManagement\">"));
41+
42+
// test <sec:csrfInput />
43+
assertTrue(body.contains("<input type=\"hidden\" name=\"_csrf\" value=\""));
44+
45+
// test <sec:csrfMetaTags />
46+
assertTrue(body.contains("<meta name=\"_csrf_parameter\" content=\"_csrf\" />"));
47+
}
48+
49+
@Test
50+
public void whenUserIsNotAuthenticatedThenOnlyAnonymousSectionsShowOnSite() throws Exception {
51+
String body = this.restTemplate.getForEntity("/", String.class)
52+
.getBody();
53+
54+
// test <sec:authorize access="!isAuthenticated()">
55+
assertTrue(body.contains("Login"));
56+
57+
// test <sec:authorize access="isAuthenticated()">
58+
assertFalse(body.contains("Logout"));
59+
}
60+
}

0 commit comments

Comments
 (0)