mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

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 logout example

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 : Customize 403 access denied page

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 Spring Security access control example

Problem Using Maven build tool, which jar should include to use HttpServletRequest or HttpServletResponse? Solution To use HttpServletRequest or HttpServletResponse in a development environment, include servlet-api. jar in your Maven pom.xml file. pom.xml <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> Put scope as provided, because most j2ee containers have this jar. You need this for complication […]

Read more Get HttpServletRequest via Maven repository

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 ClassNotFoundException : DefaultSavedRequest

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 password hashing 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 Spring Security HTTP basic authentication example

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 Get current logged in username 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 Display custom error message in Spring Security

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 Spring Security hello world example

No nonsense, four ways to loop ArrayList in Java For loop For loop (Advance) While loop Iterator loop package com.mkyong.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListLoopingExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Text 1"); list.add("Text 2"); list.add("Text 3"); System.out.println("#1 normal for loop"); for (int i = […]

Read more How to loop ArrayList in Java

In Jackson, we can use writerWithDefaultPrettyPrinter() to pretty print the JSON strings. Table of contents: 1. Setup Jackson 2. Compact print JSON (Default) 3. Pretty print JSON with Jackson 4. Pretty print JSON with Jackson (Globally) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Setup Jackson Puts jackson-databind at pom.xml. pom.xml […]

Read more How to enable pretty print JSON with Jackson

This article shows how to pretty print JSON using Gson. Table of contents: 1. Setup Google Gson 2. Default Compact Print JSON 3. Pretty Print JSON using Gson 4. Download Source Code 5. References P.S Tested with Gson 2.10.1 1. Setup Google Gson Declare gson in the pom.xml. pom.xml <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> 2. […]

Read more How to pretty print JSON using Gson

Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is an XML binding framework to convert Java objects to and from XML. XML Marshalling – Convert Java objects into XML. XML Unmarshalling – Convert XML back into Java Objects. Note This article will focus on Java 11, JAXB 3 and EclipseLink MOXy JAXB RI. […]

Read more JAXB hello world example

Spring 3, ContentNegotiatingViewResolver, is an interesting view resolver, which allow you to output a same resource (content or data) to different type of views like JSP, XML, RSS, JSON and etc. Put it simple, see following web requested URL, which will return in different views. https://mkyong.com/fruit/banana.rss , returned as RSS file. https://mkyong.com/fruit/banana.xml , returned as […]

Read more Spring 3 MVC ContentNegotiatingViewResolver example

In Spring 3, you can enable “mvc:annotation-driven” to support JSR303 bean validation via @Valid annotation, if any JSR 303 validator framework on the classpath. Note Hibernate Validator is the reference implementation for JSR 303 In this tutorial, we show you how to integrate Hibernate validator with Spring MVC, via @Valid annotation, to perform bean validation […]

Read more Spring 3 MVC and JSR303 @Valid example

In this tutorial, we show you how to output JSON data in Spring MVC framework. Technologies used : Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3 P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath. 1. Project Dependencies Get Jackson and Spring dependencies. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" […]

Read more Spring 3 MVC and JSON example

Problem Developing RSS with Spring MVC, extends “AbstractRssFeedView“, hit following error message during application start up. Caused by: java.lang.NoClassDefFoundError: com/sun/syndication/feed/WireFeed at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) at java.lang.Class.getDeclaredConstructors(Class.java:1836) //… Caused by: java.lang.ClassNotFoundException: com.sun.syndication.feed.WireFeed at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 41 more Solution Spring MVC using “ROME” to generate RSS feed. For Maven, include below dependency […]

Read more ClassNotFoundException : com.sun.syndication.feed.WireFeed

This tutorial show you how to integrate Jersey web application with Spring framework. Technologies used : Jersey 1.8 Spring 3.0.5.RELEASE Eclipse 3.6 Maven 3 1. Project Dependency Declares Jersey 1.8, Spring3 and “jersey-spring.jar” dependencies in Maven pom.xml file. Note In “jersey-spring.jar” version, it will download all the Spring 2.5.6 dependencies. To use Spring 3, you […]

Read more Jersey + Spring integration example

Problem Developing web application with Spring, make a bean with scope of “request“. @Component @Scope("request") public class PaymentService { @Autowired UserBo userBo; //… But hit following error message? Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally […]

Read more Spring request scope error : No thread-bound request found

This tutorial show you how to use Jersey client APIs to create a RESTful Java client to perform “GET” and “POST” requests to REST service that created in this “Jersey + Json” example. 1. Jersey Client Dependency To use Jersey client APIs, declares “jersey-client.jar” in your pom.xml file. File : pom.xml <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.8</version> […]

Read more RESTful Java client with Jersey client

This tutorial show you how to use JAXB to convert object to XML in Jersey, and return it back to user. 1. Dependency To integrate JAXB with Jersey, no extra dependency is required. Just include “jersey-server.jar” will do. 2. JAXB Annotation Annotate object with JAXB annotation, for conversion later. import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; […]

Read more XML example with Jersey + JAXB

In this tutorial, we show you how do to file upload with Jersey, JAX-RS implementation. 1. Jersey Multipart Dependency To support multipart (file upload) in Jersey, you just need to include “jersey-multipart.jar” in Maven pom.xml file. <project …> <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> […]

Read more File upload example in Jersey