SpringBoot with Spring Security

The aim of this post is to share the steps to create a SpringBoot web application with Security.

Tools

  • STS IDE
  • Open JDK 11

Dependencies

Create a new Spring Starter Project

SpringBoot with Spring Security Project Structure 2

Add the following dependencies to pom.xml

[code language=”xml”]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!– lookup parent from repository –>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
[/code]

View HTML files

error.html

[code language=”html”]

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:th=”https://www.thymeleaf.org”
xmlns:sec=”https://www.thymeleaf.org/thymeleaf-extras-springsecurity3″>
<head>
<meta charset=”UTF-8″>
<title>Insert title here</title>
</head>
<body>
Error
</body>
</html>

[/code]

hello.html

[code language=”html”]

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:th=”https://www.thymeleaf.org”
xmlns:sec=”https://www.thymeleaf.org/thymeleaf-extras-springsecurity3″>
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline=”text”> Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action=”@{/logout}” method=”post”>
<input type=”submit” value=”Sign out”/>
</form>
</body>
</html>

[/code]

home.html

[code language=”html”]

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:th=”https://www.thymeleaf.org”
xmlns:sec=”https://www.thymeleaf.org/thymeleaf-extras-springsecurity3″>
<head>
<meta charset=”UTF-8″>
<title>Insert title here</title>
</head>
<body>

Click <a th:href=”@{/hello}”>here</a> to see a greeting.

</body>
</html>

[/code]

login.html

[code language=”html”]

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:th=”https://www.thymeleaf.org”
xmlns:sec=”https://www.thymeleaf.org/thymeleaf-extras-springsecurity3″>
<head>
<title>Spring Security Example</title>
</head>
<body>
<div th:if=”${param.error}”>Invalid username and password.</div>
<div th:if=”${param.logout}”>You have been logged out.</div>
<form th:action=”@{/login}” method=”post”>
<div>
<label> User Name : <input type=”text” name=”username” />
</label>
</div>
<div>
<label> Password: <input type=”password” name=”password” />
</label>
</div>
<div>
<input type=”submit” value=”Sign In” />
</div>
</form>
</body>
</html>

[/code]

Spring Configuration

SpringMvcConfig

[code language=”java”]

package org.grassfield.parser;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(“/home”).setViewName(“home”);
registry.addViewController(“/”).setViewName(“home”);
registry.addViewController(“/hello”).setViewName(“hello”);
registry.addViewController(“/login”).setViewName(“login”);
registry.addViewController(“/error”).setViewName(“error”);
}
}

[/code]

Security Configuration

[code language=”java”]

package org.grassfield.parser;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(“/”,”/home”).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage(“/login”)
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username(“user”)
.password(“password”)
.roles(“USER”)
.build();

return new InMemoryUserDetailsManager(user);
}
}

[/code]

Spring Boot Application

[code language=”java”]

package org.grassfield.parser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AggregatorUiApplication {

public static void main(String[] args) {
SpringApplication.run(AggregatorUiApplication.class, args);
}

}

[/code]

Project Structure

SpringBoot with Spring Security Project Structure

Run the project as a Spring Boot application and access http://localhost:8080 in the browser.

SpringBoot with Spring Security Project Structure 3

Ref: https://spring.io/guides/gs/securing-web/ It didnt work as expected as I need to amend a few things.

I captured this Air India tail at Changi recently
I captured this Air India tail at Changi recently

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top