diff --git a/sandbox/heavyduty/certificates/Readme.txt b/sandbox/heavyduty/certificates/Readme.txt new file mode 100755 index 0000000000..64b415cf83 --- /dev/null +++ b/sandbox/heavyduty/certificates/Readme.txt @@ -0,0 +1,10 @@ +This directory contains certificates and keys for use with SSL in the sample applications. Certificates are issued by +our "Spring Security Test CA" certificate authority. + +ca.pem - the certificate authority's certificate. +server.jks - Java keystore containing the server certificate and privatekey. It Also contains the certificate authority + file and this is used as both keystore and truststore for they jetty server when running the samples with + the maven jetty plugin ("mvn jetty:run"). + +rod.p12, dianne.p12, scott.p12 are all certificate/key combinations for client authentication and can be installed in +your browser if you want to try out support for X.509 authentication. \ No newline at end of file diff --git a/sandbox/heavyduty/certificates/ca.pem b/sandbox/heavyduty/certificates/ca.pem new file mode 100755 index 0000000000..a5b52ca9d7 --- /dev/null +++ b/sandbox/heavyduty/certificates/ca.pem @@ -0,0 +1,22 @@ +-----BEGIN CERTIFICATE----- +MIIDojCCAoqgAwIBAgIEMKX1dzANBgkqhkiG9w0BAQUFADCBiTELMAkGA1UEBhMC +R0IxETAPBgNVBAgTCFNjb3RsYW5kMRAwDgYDVQQHEwdHbGFzZ293MRkwFwYDVQQK +ExBTcHJpbmcgRnJhbWV3b3JrMRgwFgYDVQQLEw9TcHJpbmcgU2VjdXJpdHkxIDAe +BgNVBAMTF1NwcmluZyBTZWN1cml0eSBUZXN0IENBMB4XDTA4MDEyNTExMTIyMVoX +DTE4MDIyNTAwMDAwMFowgYkxCzAJBgNVBAYTAkdCMREwDwYDVQQIEwhTY290bGFu +ZDEQMA4GA1UEBxMHR2xhc2dvdzEZMBcGA1UEChMQU3ByaW5nIEZyYW1ld29yazEY +MBYGA1UECxMPU3ByaW5nIFNlY3VyaXR5MSAwHgYDVQQDExdTcHJpbmcgU2VjdXJp +dHkgVGVzdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALzl/wEe +snYrwqaGZuB8hmwACtptazh1+eXCfd66FkioxlLF7yTnjCC7DT+vmMgSuThIEIsN +xlxLpEgyU3bU8GIuR8wyYIyvuSMcptdFJLV7NKYuRycxpDuqimTM7Br0nfNgKVEv +1QwguGWr6YN3aZ68/xe/D5xyPhakKu++7VFXIXw9f0+nqojdrFTqQ6l9GAVRgfX6 +h4JOaV1VFx83y2pnFj0iFneVxRcvXyWnyXlcOvJDIyVuyS/hYxb+E5rtBvp5XQ0o +5CP4OMwCZGx/jEqlL8oO7BwEgu9aEBxKvoIKJmHDTHgWIxgawTrKabmong4utnMI +yNrhsI77bmh2U7UCAwEAAaMQMA4wDAYDVR0PBAUDAwcGADANBgkqhkiG9w0BAQUF +AAOCAQEAuD8W9Ukkfyi0y65mwguFVAqBC3RSTMRXcjbLQV4rMDM/Q9kjA6acY4Ta +WgxGTwNCydqaqwDVsmn+6Je8Lp2xm9KLDLypVdNopGs+Mlfo55dhwqymXkQw1oJI +CPhR3nBmGEnSWW0UY9bPlpxRF2D5GDVwpuxDtXvWa4baPwRRI9MxwPWHA3ITl+fc +s9QVKy+pRAnuP9MSIp755cJ1CODOn2ElNCqnxxsZmcWcmI3LkHAwTmegl3PVvhrk +MKMEA/neshh/M/hWGNTFt77Hoa7pU9dv5RCWFvZPqsUgPrwGrmUvcmSDir3lSWQm +SuSED2LKVo+BFqwWS+jp49AR9b8B/Q== +-----END CERTIFICATE----- diff --git a/sandbox/heavyduty/certificates/dianne.p12 b/sandbox/heavyduty/certificates/dianne.p12 new file mode 100755 index 0000000000..6e5ba218db Binary files /dev/null and b/sandbox/heavyduty/certificates/dianne.p12 differ diff --git a/sandbox/heavyduty/certificates/rod.p12 b/sandbox/heavyduty/certificates/rod.p12 new file mode 100755 index 0000000000..4cd0564430 Binary files /dev/null and b/sandbox/heavyduty/certificates/rod.p12 differ diff --git a/sandbox/heavyduty/certificates/scott.p12 b/sandbox/heavyduty/certificates/scott.p12 new file mode 100755 index 0000000000..f0a6357e73 Binary files /dev/null and b/sandbox/heavyduty/certificates/scott.p12 differ diff --git a/sandbox/heavyduty/certificates/server.jks b/sandbox/heavyduty/certificates/server.jks new file mode 100755 index 0000000000..f56cf2e837 Binary files /dev/null and b/sandbox/heavyduty/certificates/server.jks differ diff --git a/sandbox/heavyduty/pom.xml b/sandbox/heavyduty/pom.xml new file mode 100755 index 0000000000..a30400feb2 --- /dev/null +++ b/sandbox/heavyduty/pom.xml @@ -0,0 +1,166 @@ + + 4.0.0 + org.springframework.security + spring-security-heavyduty + Spring Security - Heavy Duty Sample + war + 2.0.0 + + + org.springframework.security + spring-security-core + ${spring.security.version} + + + org.springframework.security + spring-security-core-tiger + ${spring.security.version} + + + org.springframework.security + spring-security-taglibs + ${spring.security.version} + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-jdbc + runtime + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + org.springframework + spring-aop + runtime + ${spring.version} + + + hsqldb + hsqldb + 1.8.0.7 + compile + + + org.hibernate + hibernate-entitymanager + 3.3.2.GA + compile + + + net.sf.ehcache + ehcache + 1.3.0 + compile + + + org.aspectj + aspectjweaver + true + 1.5.4 + + + org.aspectj + aspectjrt + 1.5.4 + + + javax.servlet + servlet-api + provided + 2.4 + + + javax.servlet + jstl + runtime + 1.1.2 + + + taglibs + standard + runtime + 1.1.2 + + + log4j + log4j + runtime + 1.2.14 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.5.1 + + true + 2.0 + + + + org.mortbay.jetty + maven-jetty-plugin + 6.1.7 + + /tutorial + + + 8080 + 8443 + + + 8443 + certificates/server.jks + password + password + certificates/server.jks + password + true + false + + + + + + + + 2.5.3 + 2.0.1-SNAPSHOT + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/java/bigbank/Account.java b/sandbox/heavyduty/src/main/java/bigbank/Account.java new file mode 100755 index 0000000000..1fdc1044e5 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/Account.java @@ -0,0 +1,51 @@ +package bigbank; + +/** + * Note this class does not represent best practice, as we are failing to + * encapsulate business logic (methods) and state in the domain object. + * Nevertheless, this demo is intended to reflect what people usually do, + * as opposed to what they ideally would be doing. + * + * @author Ben Alex + * @version $Id$ + */ +public class Account { + private long id = -1; + private String holder; + private double balance; + + public Account(String holder) { + super(); + this.holder = holder; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getHolder() { + return holder; + } + + public void setHolder(String holder) { + this.holder = holder; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double balance) { + this.balance = balance; + } + + public String toString() { + return "Account[id=" + id + ",balance=" + balance +",holder=" + holder + "]"; + } + + +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/BankDao.java b/sandbox/heavyduty/src/main/java/bigbank/BankDao.java new file mode 100755 index 0000000000..67806a7e76 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/BankDao.java @@ -0,0 +1,7 @@ +package bigbank; + +public interface BankDao { + public Account readAccount(Long id); + public void createOrUpdateAccount(Account account); + public Account[] findAccounts(); +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/BankDaoStub.java b/sandbox/heavyduty/src/main/java/bigbank/BankDaoStub.java new file mode 100755 index 0000000000..de46d38af9 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/BankDaoStub.java @@ -0,0 +1,32 @@ +package bigbank; + +import java.util.HashMap; +import java.util.Map; + +public class BankDaoStub implements BankDao { + private long id = 0; + private Map accounts = new HashMap(); + + public void createOrUpdateAccount(Account account) { + if (account.getId() == -1) { + id++; + account.setId(id); + } + accounts.put(new Long(account.getId()), account); + System.out.println("SAVE: " + account); + } + + public Account[] findAccounts() { + Account[] a = (Account[]) accounts.values().toArray(new Account[] {}); + System.out.println("Returning " + a.length + " account(s):"); + for (int i = 0; i < a.length; i++) { + System.out.println(" > " + a[i]); + } + return a; + } + + public Account readAccount(Long id) { + return (Account) accounts.get(id); + } + +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/BankService.java b/sandbox/heavyduty/src/main/java/bigbank/BankService.java new file mode 100755 index 0000000000..90c21ccde5 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/BankService.java @@ -0,0 +1,15 @@ +package bigbank; + +import org.springframework.security.annotation.Secured; + +public interface BankService { + + @Secured("IS_AUTHENTICATED_ANONYMOUSLY") + public Account readAccount(Long id); + + @Secured("IS_AUTHENTICATED_ANONYMOUSLY") + public Account[] findAccounts(); + + @Secured("ROLE_TELLER") + public Account post(Account account, double amount); +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/BankServiceImpl.java b/sandbox/heavyduty/src/main/java/bigbank/BankServiceImpl.java new file mode 100755 index 0000000000..e461e132d0 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/BankServiceImpl.java @@ -0,0 +1,40 @@ +package bigbank; + +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.util.Assert; + +public class BankServiceImpl implements BankService { + private BankDao bankDao; + + // Not used unless you declare a + @Pointcut("execution(* bigbank.BankServiceImpl.*(..))") + public void myPointcut() {} + + public BankServiceImpl(BankDao bankDao) { + Assert.notNull(bankDao); + this.bankDao = bankDao; + } + + public Account[] findAccounts() { + return this.bankDao.findAccounts(); + } + + public Account post(Account account, double amount) { + Assert.notNull(account); + Assert.notNull(account.getId()); + + // We read account bank from DAO so it reflects the latest balance + Account a = bankDao.readAccount(account.getId()); + if (account == null) { + throw new IllegalArgumentException("Couldn't find requested account"); + } + + a.setBalance(a.getBalance() + amount); + bankDao.createOrUpdateAccount(a); + return a; + } + + public Account readAccount(Long id) { + return bankDao.readAccount(id); + } +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/SeedData.java b/sandbox/heavyduty/src/main/java/bigbank/SeedData.java new file mode 100755 index 0000000000..5bf0774448 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/SeedData.java @@ -0,0 +1,21 @@ +package bigbank; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +public class SeedData implements InitializingBean{ + private BankDao bankDao; + + public void afterPropertiesSet() throws Exception { + Assert.notNull(bankDao); + bankDao.createOrUpdateAccount(new Account("rod")); + bankDao.createOrUpdateAccount(new Account("dianne")); + bankDao.createOrUpdateAccount(new Account("scott")); + bankDao.createOrUpdateAccount(new Account("peter")); + } + + public void setBankDao(BankDao bankDao) { + this.bankDao = bankDao; + } + +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/web/ListAccounts.java b/sandbox/heavyduty/src/main/java/bigbank/web/ListAccounts.java new file mode 100755 index 0000000000..d0ea3ed67b --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/web/ListAccounts.java @@ -0,0 +1,34 @@ +package bigbank.web; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.AuthenticationCredentialsNotFoundException; +import org.springframework.util.Assert; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + +import bigbank.BankService; + +public class ListAccounts implements Controller { + + private BankService bankService; + + public ListAccounts(BankService bankService) { + Assert.notNull(bankService); + this.bankService = bankService; + } + + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { + // Security check (this is unnecessary if Spring Security is performing the authorization) +// if (request.getUserPrincipal() == null) { +// throw new AuthenticationCredentialsNotFoundException("You must login to view the account list (Spring Security message)"); // only for Spring Security managed authentication +// } + + // Actual business logic + ModelAndView mav = new ModelAndView("listAccounts"); + mav.addObject("accounts", bankService.findAccounts()); + return mav; + } + +} diff --git a/sandbox/heavyduty/src/main/java/bigbank/web/PostAccounts.java b/sandbox/heavyduty/src/main/java/bigbank/web/PostAccounts.java new file mode 100755 index 0000000000..e5967b52e3 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/bigbank/web/PostAccounts.java @@ -0,0 +1,39 @@ +package bigbank.web; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.AccessDeniedException; +import org.springframework.util.Assert; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + +import bigbank.Account; +import bigbank.BankService; + +public class PostAccounts implements Controller { + + private BankService bankService; + + public PostAccounts(BankService bankService) { + Assert.notNull(bankService); + this.bankService = bankService; + } + + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { + // Security check (this is unnecessary if Spring Security is performing the authorization) +// if (!request.isUserInRole("ROLE_TELLER")) { +// throw new AccessDeniedException("You must be a teller to post transactions (Spring Security message)"); // only for Spring Security managed authentication +// } + + // Actual business logic + Long id = ServletRequestUtils.getRequiredLongParameter(request, "id"); + Double amount = ServletRequestUtils.getRequiredDoubleParameter(request, "amount"); + Account a = bankService.readAccount(id); + bankService.post(a, amount); + + return new ModelAndView("redirect:listAccounts.html"); + } + +} diff --git a/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyAuthenticationProcessingFilter.java b/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyAuthenticationProcessingFilter.java new file mode 100755 index 0000000000..a456c7fad9 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyAuthenticationProcessingFilter.java @@ -0,0 +1,11 @@ +package heavyduty.security.ui; + +import org.springframework.security.ui.webapp.AuthenticationProcessingFilter; + +public class HeavyDutyAuthenticationProcessingFilter extends AuthenticationProcessingFilter { + + + + + +} diff --git a/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyEntryPoint.java b/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyEntryPoint.java new file mode 100755 index 0000000000..3ee843792c --- /dev/null +++ b/sandbox/heavyduty/src/main/java/heavyduty/security/ui/HeavyDutyEntryPoint.java @@ -0,0 +1,9 @@ +package heavyduty.security.ui; + +import org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint; + +public class HeavyDutyEntryPoint extends AuthenticationProcessingFilterEntryPoint { + + + +} diff --git a/sandbox/heavyduty/src/main/java/sample/dao/GenericDAO.java b/sandbox/heavyduty/src/main/java/sample/dao/GenericDAO.java new file mode 100755 index 0000000000..4272790621 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/dao/GenericDAO.java @@ -0,0 +1,46 @@ +package sample.dao; + +import java.io.Serializable; + + +/** + * The Interface GenericDAO. + */ +public interface GenericDAO +{ + /** + * persist + * @param transientInstance objet to persist + */ + void persist(T transientInstance); + + + /** + * refresh + * @param instance objet to refresh + */ + void refresh(T instance); + + + /** + * delete + * @param persistentInstance objet to delete + */ + void delete(T persistentInstance); + + + /** + * merge + * @param detachedInstance objet to merge + * @return merged object + */ + T merge(T detachedInstance); + + + /** + * read + * @param id of object to read + * @return read object + */ + T read(PK id); +} \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/java/sample/dao/UserDAO.java b/sandbox/heavyduty/src/main/java/sample/dao/UserDAO.java new file mode 100755 index 0000000000..5017a149f3 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/dao/UserDAO.java @@ -0,0 +1,13 @@ + +package sample.dao; + +import sample.domain.User; + + +/** + * The Interface PatientDAO. + */ +public interface UserDAO extends GenericDAO { + + public User findByUsername(String username); +} diff --git a/sandbox/heavyduty/src/main/java/sample/dao/impl/GenericDAOImpl.java b/sandbox/heavyduty/src/main/java/sample/dao/impl/GenericDAOImpl.java new file mode 100755 index 0000000000..4fffeb7ef3 --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/dao/impl/GenericDAOImpl.java @@ -0,0 +1,126 @@ +package sample.dao.impl; + +import java.io.Serializable; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import sample.dao.GenericDAO; + + +public class GenericDAOImpl + implements GenericDAO { + /** type */ + private Class type; + + /** the logger */ + private static final Log LOG = LogFactory.getLog(GenericDAOImpl.class); + + @PersistenceContext + private EntityManager entityManager; + + /** + * Minimal constructor + * + * @param t + * type POJO hibernate + */ + public GenericDAOImpl(Class t) { + this.type = t; + } + + /** + * read data + * + * @param id + * data id + * @return data + */ + @SuppressWarnings("unchecked") + public T read(PK id) { + if (id == null) { + throw new IllegalArgumentException("Id cannot be null or empty"); + } + + // find() au lieu de getReference() pour forcer l'initialisation de + // l'objet, sinon on ne recupère + // qu'un proxy non-initialisé ! + return entityManager.find(type, id); + + } + + /** + * persist data + * + * @param transientInstance + * data to persist + * @see sido.common.dao.GenericDAO#persist(T) + */ + public void persist(T transientInstance) { + if (LOG.isDebugEnabled()) { + LOG.debug("Persisting instance of " + + transientInstance.getClass().getSimpleName()); + } + entityManager.persist(transientInstance); + } + + /** + * + * attach clean + * + * @param instance + * data to attach + * @see sido.common.dao.GenericDAO#refresh(T) + */ + public void refresh(T instance) { + if (LOG.isDebugEnabled()) { + LOG.debug("refreshing instance of " + + instance.getClass().getSimpleName()); + } + entityManager.refresh(instance); + } + + /** + * delete + * + * @param persistentInstance + * data to delete + * @see sido.common.dao.GenericDAO#delete(T) + */ + public void delete(T persistentInstance) { + if (LOG.isDebugEnabled()) { + LOG.debug("deleting instance of " + + persistentInstance.getClass().getSimpleName()); + } + entityManager.remove(persistentInstance); + } + + /** + * merge + * + * @param detachedInstance + * data to merge + * @return the merged data + * @see sido.common.dao.GenericDAO#merge(T) + */ + @SuppressWarnings("unchecked") + public T merge(T detachedInstance) { + if (LOG.isDebugEnabled()) { + LOG.debug("merging instance of " + + detachedInstance.getClass().getSimpleName()); + } + return entityManager.merge(detachedInstance); + } + + /** + * @return the entityManager + */ + public EntityManager getEntityManager() { + return entityManager; + } + + +} \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/java/sample/dao/impl/UserDAOImpl.java b/sandbox/heavyduty/src/main/java/sample/dao/impl/UserDAOImpl.java new file mode 100755 index 0000000000..0712ab455f --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/dao/impl/UserDAOImpl.java @@ -0,0 +1,27 @@ +package sample.dao.impl; + +import org.springframework.stereotype.Repository; + +import sample.domain.User; + +/** + * UserDAOImpl + */ +@Repository +public class UserDAOImpl extends GenericDAOImpl implements + sample.dao.UserDAO { + + /** + * Required constructor + */ + public UserDAOImpl() { + super(User.class); + } + + public User findByUsername(String username) { + return (User) getEntityManager().createNamedQuery("User.findByUsername") + .setParameter("username", username).getSingleResult(); + } + + +} diff --git a/sandbox/heavyduty/src/main/java/sample/domain/User.java b/sandbox/heavyduty/src/main/java/sample/domain/User.java new file mode 100755 index 0000000000..7fc7bf6afa --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/domain/User.java @@ -0,0 +1,106 @@ + + +package sample.domain; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; + +/** + * The Class Patient. + */ +@Entity +@NamedQuery(name = "User.findByUsername", query = "from User where username= :username") +public class User implements Serializable { + + /** serialVersionUID */ + private static final long serialVersionUID = 7073017148588882593L; + + /** The id. */ + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + private Long id; + + /** The username. */ + @Basic(optional = false) + private String username; + + /** The username. */ + @Basic(optional = false) + private String password; + + /** + * Default constructor + */ + public User() { + super(); + } + + /** + * @param username + * @param password + */ + public User(String username, String password) { + super(); + this.username = username; + this.password = password; + } + + /** + * @return the id + */ + public Long getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Long id) { + this.id = id; + } + + /** + * @return the username + */ + public String getUsername() { + return username; + } + + /** + * @param username the username to set + */ + public void setUsername(String username) { + this.username = username; + } + + /** + * Full constructor + * @param username + */ + public User(String username, String password, Date derniereConnexion, + String key) { + super(); + this.username = username; + } + + /** + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } +} diff --git a/sandbox/heavyduty/src/main/java/sample/service/UserService.java b/sandbox/heavyduty/src/main/java/sample/service/UserService.java new file mode 100755 index 0000000000..7130c120ee --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/service/UserService.java @@ -0,0 +1,16 @@ +package sample.service; + +import org.springframework.security.userdetails.UserDetails; +import org.springframework.security.userdetails.UserDetailsService; + +public interface UserService extends UserDetailsService { + + /** + * Register a new User in database + * @param username + */ + public UserDetails register(String username, String password); + + + +} diff --git a/sandbox/heavyduty/src/main/java/sample/service/impl/UserServiceImpl.java b/sandbox/heavyduty/src/main/java/sample/service/impl/UserServiceImpl.java new file mode 100755 index 0000000000..8730c6888e --- /dev/null +++ b/sandbox/heavyduty/src/main/java/sample/service/impl/UserServiceImpl.java @@ -0,0 +1,68 @@ +/** + * + */ +package sample.service.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.AuthenticationException; +import org.springframework.security.GrantedAuthority; +import org.springframework.security.GrantedAuthorityImpl; +import org.springframework.security.userdetails.UserDetails; +import org.springframework.security.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import sample.dao.UserDAO; +import sample.domain.User; +import sample.service.UserService; + +/** + * @author A207119 + * + */ +@Component +@Transactional +public class UserServiceImpl implements UserService { + + /** The logger */ + private static final Log LOG = LogFactory.getLog(UserServiceImpl.class); + + /** The User DAO */ + @Autowired + private UserDAO userDAO = null; + + public UserDetails loadUserByUsername(String username) + throws AuthenticationException { + try { + User user = userDAO.findByUsername(username); + + return new org.springframework.security.userdetails.User(user + .getUsername(), user.getPassword(), true, true, true, true, + new GrantedAuthority[] { new GrantedAuthorityImpl("ROLE_USER") }); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + throw new UsernameNotFoundException("No matching account", e); + } + } + + public UserDetails register(String username, String password) { + User user = new User(username, password); + userDAO.persist(user); + return new org.springframework.security.userdetails.User(user + .getUsername(), user.getPassword(), true, true, true, true, + new GrantedAuthority[] { new GrantedAuthorityImpl("ROLE_USER") }); + + } + + /** + * @param userDAO + * the userDAO to set + */ + public void setUserDAO(UserDAO userDAO) { + this.userDAO = userDAO; + } + +} diff --git a/sandbox/heavyduty/src/main/resources/applicationContext-business.xml b/sandbox/heavyduty/src/main/resources/applicationContext-business.xml new file mode 100755 index 0000000000..e1e21a97e4 --- /dev/null +++ b/sandbox/heavyduty/src/main/resources/applicationContext-business.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/META-INF/MANIFEST.MF b/sandbox/heavyduty/src/main/webapp/META-INF/MANIFEST.MF new file mode 100755 index 0000000000..58630c02ef --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-misc.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-misc.xml new file mode 100755 index 0000000000..67da7b4a52 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-misc.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + My Realm + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-persistence.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-persistence.xml new file mode 100755 index 0000000000..b1dd610e78 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-persistence.xml @@ -0,0 +1,59 @@ + + + + + + classpath:jdbc.properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-security.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-security.xml new file mode 100755 index 0000000000..055b910218 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/appContext-security.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/applicationContext-acegi-security.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/applicationContext-acegi-security.xml new file mode 100755 index 0000000000..edd0759a43 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/applicationContext-acegi-security.xml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + My Realm + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/bank-servlet.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/bank-servlet.xml new file mode 100755 index 0000000000..a119417066 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/bank-servlet.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/META-INF/persistence.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/META-INF/persistence.xml new file mode 100755 index 0000000000..20e12ff1bf --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,24 @@ + + + + org.hibernate.ejb.HibernatePersistence + sample.domain.User + + + + + + + + + + + + diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/jdbc.properties b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/jdbc.properties new file mode 100755 index 0000000000..a88ac1e426 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/jdbc.properties @@ -0,0 +1,8 @@ +jpa.dialect=org.hibernate.dialect.HSQLDialect +jpa.generateDdl=true +jpa.showSql=true + +jdbc.driver=org.hsqldb.jdbcDriver +jdbc.url=jdbc:hsqldb:mem:. +jdbc.username=sa +jdbc.password= \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/log4j.properties b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/log4j.properties new file mode 100755 index 0000000000..b2a7f64139 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/log4j.properties @@ -0,0 +1,18 @@ +# Global logging configuration +log4j.rootLogger=DEBUG, stdout + +log4j.logger.org.springframework.security=DEBUG, stdout + +# Console output... +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.conversionPattern=[%p,%c{1},%L] - %m%n + +# Rolling log file output... +#log4j.appender.fileout=org.apache.log4j.RollingFileAppender +#log4j.appender.fileout.File=spring-security-tutorial.log +#log4j.appender.fileout.File=${webapp.root}/WEB-INF/log4j.log +#log4j.appender.fileout.MaxFileSize=1024KB +#log4j.appender.fileout.MaxBackupIndex=1 +#log4j.appender.fileout.layout=org.apache.log4j.PatternLayout +#log4j.appender.fileout.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1},%t:%L - %m%n diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/users.ldif b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/users.ldif new file mode 100755 index 0000000000..0cf02e22ec --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/classes/users.ldif @@ -0,0 +1,60 @@ +dn: ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: groups + +dn: ou=people,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: people + +dn: uid=rod,ou=people,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Rod Johnson +sn: Johnson +uid: rod +userPassword: koala + +dn: uid=dianne,ou=people,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Dianne Emu +sn: Emu +uid: dianne +userPassword: emu + +dn: uid=scott,ou=people,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Scott +sn: Wombat +uid: scott +userPassword: wombat + +dn: cn=user,ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: groupOfNames +cn: user +member: uid=rod,ou=people,dc=springframework,dc=org +member: uid=dianne,ou=people,dc=springframework,dc=org +member: uid=scott,ou=people,dc=springframework,dc=org + +dn: cn=teller,ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: groupOfNames +cn: teller +member: uid=rod,ou=people,dc=springframework,dc=org +member: dianne=rod,ou=people,dc=springframework,dc=org + +dn: cn=supervisor,ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: groupOfNames +cn: supervisor +member: uid=rod,ou=people,dc=springframework,dc=org diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/jsp/listAccounts.jsp b/sandbox/heavyduty/src/main/webapp/WEB-INF/jsp/listAccounts.jsp new file mode 100755 index 0000000000..548f43f3ba --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/jsp/listAccounts.jsp @@ -0,0 +1,27 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%> + +

Accounts

+ +Home3

+ + + + + + + + + + +
+ + + + + + + &amount=-20.00">-$20 + &amount=-5.00">-$5 + &amount=5.00">+$5 + &amount=20.00">+$20 +
\ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/WEB-INF/web.xml b/sandbox/heavyduty/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 0000000000..e745ff4d3c --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,81 @@ + + + + + + Spring Security Tutorial Application + + + + contextConfigLocation + + classpath:applicationContext-business.xml + /WEB-INF/appContext-persistence.xml + /WEB-INF/appContext-security.xml + + + + + log4jConfigLocation + /WEB-INF/classes/log4j.properties + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + + springSecurityFilterChain + /* + + + + org.springframework.web.util.Log4jConfigListener + + + + + org.springframework.web.context.ContextLoaderListener + + + + + org.springframework.security.ui.session.HttpSessionEventPublisher + + + + + bank + org.springframework.web.servlet.DispatcherServlet + 1 + + + + bank + *.html + + + + index.jsp + + + diff --git a/sandbox/heavyduty/src/main/webapp/index.jsp b/sandbox/heavyduty/src/main/webapp/index.jsp new file mode 100755 index 0000000000..edf1d00d0b --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/index.jsp @@ -0,0 +1,18 @@ +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + +

Home Page

+

+Anyone can view this page. +

+

+If you're logged in, you can list accounts. +

+

+Your principal object is....: <%= request.getUserPrincipal() %> +

+ +

Secure page

+

Extremely secure page

+ + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/login.jsp b/sandbox/heavyduty/src/main/webapp/login.jsp new file mode 100755 index 0000000000..d752d3975e --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/login.jsp @@ -0,0 +1,47 @@ +<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt' %> +<%@ page import="org.springframework.security.ui.AbstractProcessingFilter" %> +<%@ page import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter" %> +<%@ page import="org.springframework.security.AuthenticationException" %> + + + + + + CUSTOM SPRING SECURITY LOGIN + + + +

CUSTOM SPRING SECURITY LOGIN

+ +

Valid users: +

+

username rod, password koala +
username dianne, password emu +
username scott, password wombat +
username peter, password opal +

+ + <%-- this form-login-page form is also used as the + form-error-page to ask for a login again. + --%> + <% if (session.getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY) != null) { %> + + Your login attempt was not successful, try again.

+ Reason: <%= ((AuthenticationException) session.getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY)).getMessage() %> +
+ <% } %> + +

+ + + + + + + +
User:value='<%= session.getAttribute(AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY) %>'<% } %>>
Password:
Don't ask for my password for two weeks
+ +
+ + + diff --git a/sandbox/heavyduty/src/main/webapp/secure/extreme/index.jsp b/sandbox/heavyduty/src/main/webapp/secure/extreme/index.jsp new file mode 100755 index 0000000000..93f7a17cfc --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/secure/extreme/index.jsp @@ -0,0 +1,15 @@ +<%@ taglib prefix="authz" uri="http://www.springframework.org/security/tags" %> + + + +

VERY Secure Page

+This is a protected page. You can only see me if you are a supervisor. + + + You have "ROLE_SUPERVISOR" (this text is surrounded by <authz:authorize> tags). + + +

Home +

Logout + + \ No newline at end of file diff --git a/sandbox/heavyduty/src/main/webapp/secure/index.jsp b/sandbox/heavyduty/src/main/webapp/secure/index.jsp new file mode 100755 index 0000000000..e44be8bcd7 --- /dev/null +++ b/sandbox/heavyduty/src/main/webapp/secure/index.jsp @@ -0,0 +1,36 @@ +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + +

Secure Page

+

+This is a protected page. You can get to me if you've been remembered, +or if you've authenticated this session. +

+ + + You are a supervisor! You can therefore see the extremely secure page.

+
+ +

Properties obtained using <sec:authentication /> tag

+ + + + + + + + + + + + + + +
TagValue
<sec:authentication property='name' />
<sec:authentication property='principal.username' />
<sec:authentication property='principal.enabled' />
<sec:authentication property='principal.accountNonLocked' />
+ + +

Home +

Logout + + \ No newline at end of file