Java Password Hashing with Argon2

Argon2 was the winner of the Password Hashing Competition in July 2015, a one-way hashing function that is intentionally resource (CPU, memory, etc) intensive. In Argon2, we can configure the length of the salt, the length of the generated hash, iterations, memory cost, and CPU cost to control the resources that are needed to hash …

Read more

Spring REST + Spring Security Example

In this article, we will enhance the previous Spring REST Validation Example, by adding Spring Security to perform authentication and authorization for the requested URLs (REST API endpoints) Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Spring Security 5.1.3.RELEASE Spring Data JPA 2.1.4.RELEASE H2 In-memory Database 1.4.197 Tomcat Embed 9.0.14 JUnit 4.12 Maven 3 Java …

Read more

Spring Security – There is no PasswordEncoder mapped for the id “null”

Send a GET request with username and password, but hits the password encoder error? Tested Spring Boot 2.1.2.RELEASE 5.1.3.RELEASE $ curl localhost:8080/books -u user:password { "timestamp":"2019-02-22T15:03:49.322+0000", "status":500, "error":"Internal Server Error", "message":"There is no PasswordEncoder mapped for the id \"null\"", "path":"/books" } errors in logs java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" Here …

Read more

Spring Security + Hibernate Annotation Example

In this tutorial, previous Spring Security + Hibernate4 XML example will be reused, and convert it to a annotation-based example. Technologies used : Spring 3.2.8.RELEASE Spring Security 3.2.3.RELEASE Hibernate 4.2.11.Final MySQL Server 5.6 Tomcat 7 (Servlet 3.x container) Quick Note : Create a session factory with LocalSessionFactoryBuilder Inject session factory into a UserDao Integrate UserDao …

Read more

Spring Security + Hibernate XML Example

In this tutorial, we will show you how to integrate Hibernate 4 in Spring Security, XML configuration example. Note For annotation version, please read this Spring Security + Hibernate Annotation Example. Technologies used : Spring 3.2.8.RELEASE Spring Security 3.2.3.RELEASE Hibernate 4.2.11.Final MySQL Server 5.6 JDK 1.6 Maven 3 Eclipse 4.3 Quick Notes Create a session …

Read more

Spring Security : Encoded password does not look like BCrypt

In Spring Security, database authentication with bcrypt password hashing. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; //… String password = “123456”; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String hashedPassword = passwordEncoder.encode(password); spring-security.xml <authentication-manager> <authentication-provider> <password-encoder hash="bcrypt" /> //… </authentication-provider> </authentication-manager> CREATE TABLE users ( username VARCHAR(45) NOT NULL , password VARCHAR(45) NOT NULL , enabled TINYINT NOT NULL DEFAULT …

Read more

Spring Security : Check if user is from remember me cookie

This Spring Security example shows you how to check if a user is login from a “remember me” cookie. private boolean isRememberMeAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return false; } return RememberMeAuthenticationToken.class.isAssignableFrom(authentication.getClass()); } @RequestMapping(value = "/admin/update**", method = RequestMethod.GET) public ModelAndView updatePage() { ModelAndView model = new ModelAndView(); if (isRememberMeAuthenticated()) …

Read more

Spring Security Hello World Annotation Example

In preview post, we are using XML files to configure the Spring Security in a Spring MVC environment. In this tutorial, we are going to show you how to convert the previous XML-base Spring Security project into a pure Spring annotation project. Technologies used : Spring 3.2.8.RELEASE Spring Security 3.2.3.RELEASE Eclipse 4.2 JDK 1.6 Maven …

Read more

Spring Security logout example

In Spring Security, to log out, just add a link to url “j_spring_security_logout“, for example : <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body> <h2>messages, whatever</h2> <a href="<c:url value="j_spring_security_logout" />" > Logout</a> </body> </html> In Spring security, declares “logout” tag, and configure the “logout-success-url” attribute : <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <http auto-config="true"> <intercept-url …

Read more

Spring Security : Customize 403 access denied page

In Spring Security, if non authorized user try to access a protected page, a default “http 403 access denied” will be displayed : In this tutorial, we will show you how to customize 403 access denied page in Spring Security. 1. Spring Security Configuration Review a configuration, if “alex” try to access /admin page, above …

Read more

Spring Security access control example

In Spring Security, access control or authorization is easy to implement. See following code snippet : <http auto-config="true"> <intercept-url pattern="/admin*" access="ROLE_ADMIN" /> </http> It means, only user with authority of “ROLE_ADMIN” is allow to access URI /admin*. If non authorized user try to access it, a “http 403 access denied page” will be displayed. Spring …

Read more

ClassNotFoundException : DefaultSavedRequest

Problem Working with Spring Security, which jar contains DefaultSavedRequest? SEVERE: Exception loading sessions from persistent storage java.lang.ClassNotFoundException: org.springframework.security.web.savedrequest.DefaultSavedRequest at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) Solution DefaultSavedRequest is inside spring-security-web.jar. Visit this Spring Security hello world example for the list of dependencies libraries. <!– Spring Security & dependencies –> <dependency> <groupId>org.springframework.security</groupId> …

Read more

Spring Security password hashing example

In this tutorial, we will show you how to use BCryptPasswordEncoder to hash a password and perform a login authentication in Spring Security. In the old days, normally, we used MD5 Md5PasswordEncoder or SHA ShaPasswordEncoder hashing algorithm to encode a password… you are still allowed to use whatever encoder you like, but Spring recommends to …

Read more

Spring Security HTTP basic authentication example

When HTTP basic authentication is configured, web browser will display a login dialog for user authentication. This tutorial show you how to configure HTTP basic authentication in Spring Security. <http> <intercept-url pattern="/welcome*" access="ROLE_USER" /> <http-basic /> </http> Last Spring Security form-based login example will be reused, but switch authentication to support HTTP basic. 1. Spring …

Read more

Get current logged in username in Spring Security

In this article, we will show you three ways to get the current logged in username in Spring Security. 1. SecurityContextHolder + Authentication.getName() import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class LoginController { @RequestMapping(value="/login", method = RequestMethod.GET) public String printUser(ModelMap model) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name …

Read more

Display custom error message in Spring Security

In Spring Security, when authentication is failed, following predefined error messages will be displayed : Spring display : Bad credentials In this article, we show you how to override above error message and display your custom error message. For example, Spring display : Bad credentials You want override it with this message : Invalid username …

Read more

Spring Security hello world example

In this tutorial, we will show you how to integrate Spring Security with a Spring MVC web application to secure a URL access. After implementing Spring Security, to access the content of an “admin” page, users need to key in the correct “username” and “password”. Technologies used : Spring 3.2.8.RELEASE Spring Security 3.2.3.RELEASE Eclipse 4.2 …

Read more