Skip to content

Instantly share code, notes, and snippets.

<!-- Spring MVC -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
@bytestree
bytestree / spring-mvc-servlet.xml
Last active May 6, 2016 13:02
Spring context file to configure property placeholder, resource bundles, view resolver and import other configutation files.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bytestree.*" />
@bytestree
bytestree / spring-security.xml
Created April 23, 2016 13:46
Spring Secuirty file to configure url-patterns, login, logout and error page. Configure handler for successful and unsuccessful login. Authentication manager and provider.
<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-4.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">
<http auto-config="true">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
@bytestree
bytestree / CustomAuthenticationFailureHandler.java
Last active November 22, 2019 16:32
UserService to return UserDetails object on authentication and implementation of AuthenticationSuccessHandler and AuthenticationFailureHandler
@Component("customAuthenticationFailureHandler")
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
private String DEFAULT_FAILURE_URL = "/login?error";
@Autowired
private UserService userService;
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
@bytestree
bytestree / Roles.java
Created April 23, 2016 16:31
Model classes and database script for spring authentication
package com.bytestree.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@bytestree
bytestree / login.jsp
Last active May 6, 2016 12:58
Custom Login page for Spring Security 4 including CSRF token config
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Spring Security</title>
</head>
<body>
<div>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="false"%>
<html>
<body>
<h1>Home Page</h1>
<h2>${message}</h2>
<a href='<c:url value="/admin" />'>Admin Page</a>
@bytestree
bytestree / AbstractDao.java
Last active May 6, 2016 12:56
Generic DAO layer and configuration of Transaction Manager, SessionFactory in Hibernate for PostgreSQL
package com.bytestree.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
@SuppressWarnings("unchecked")
@bytestree
bytestree / messages.properties
Created April 24, 2016 15:27
override spring security error messages
AbstractUserDetailsAuthenticationProvider.badCredentials=Invalid username or password
AbstractUserDetailsAuthenticationProvider.locked=Your account is locked
AbstractUserDetailsAuthenticationProvider.disabled=Your account is disabled
@bytestree
bytestree / pom.xml
Created May 7, 2016 12:51
Hibernate Generic DAO dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bytestree.hibernate</groupId>
<artifactId>generic-dao-hibernate</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<hibernate.version>4.3.11.Final</hibernate.version>