[Introduction]
For a typical web application, perfect authentication and authorization mechanisms are essential, in SpringFramework, the sample jpetstore provided by Juergen Hoeller gives some introductions, but far is not enough, Acegi is a specialized for SpringFramework Projects of the security mechanism, all called Acegi Security System for Spring, the current version is 0.5.1, and the functionality that is currently available should meet the needs of most applications. The main purpose of this article is to explain how to use Acegi in a Spring architecture, rather than detail each interface, each class. Note that even if the Spring application already exists, you can immediately enjoy the authentication and authorization provided by ACEGI immediately.
[basic work]
Add acegi-security.jar in the Acegi Download Pack in the LIB of your web application.
[Web.xml]
The most common way to achieve authentication and authorization is through Filter, Acegi is also the case, usually Acegi needs to add the following five filters to Web.xml:
acegi channel processing filter filter-name> net.sf.acegisecurity.util.FiltertobeAnProxy filter-class> targetclass < / param-name> net.sf.acegisecurity.securechannel.ChannelProcessingFilter param-value> init-param> filter> Acegi Authentication Processing filter filter -Name> net.sf.acegisecurity.util.FilTerTobeAnProxy filter-class> targetclass param-name> net.sf.acegisecurity .ui.webapp.authenticationProcessingFilter param-value> acegi http Basic Authorization filter filter-name> net.sf. acegisecurity.util.FilterToBeanProxy filter-class> targetClass param-name> net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter param-value> init-param> filter> acegi security system for Spring Auto Integration Filter filter-name> net.sf.acegisecurity.ui.AutoIntegrationFilter filter-class> filter> Acegi HTTP Request Security Filter filter-name > net.sf.acegisecurity.util.FilTertobeAnProxy filter-class> targetclass net.sf.acegisecurity.intercept .web.securityenforcementfilter param-value> init-param> <
/ Filter> The first cause of confusion is net.sf.acegisecurity.util.FilterToBeanProxy, on Acegi own document explains that:. "What FilterToBeanProxy does is delegate the Filter's methods through to a bean which is obtained from the Spring application context This Enables the bean to benefit from the Spring Application Context Lifecy. ", if you want to study, go see the source code should not understand. Then add filter-mapping: acegi channel processing filter filter-name> / * url-pattern> filter-mapping> acegi authentication processing filter filter-name> / * url-pattern> filter-mapping> acegi http Basic Authorization filter filter-name> / * url-pattern> filter-maping> acegi security system for Spring Auto Integration Filter filter-name > / * url-pattern> filter-mapping> acegi http request security filter filter-name> / * URL-PATTERN> filter-maping> Here you need to pay attention to the following two points: 1) These Filter's order cannot be changed, the order is not working properly; 2) If your application does not require secure transmission, such as HTTPS, then release the "Acegi Channel Processing Filter" can be released. can.
[ApplicationContext.xml] The next thing to add ApplicationContext.xml, from the explanation of FilTerTobeanFactory, you can see that true Filter is managed in Spring ApplicationContext: 1) First, you must have saved users in your database. Name and password Table, Acegi requires Table's schema must be as follows: Create Table Users (Username Varchar (50) Not Null Primary Key, Password VARCHAR (50) Not Null; Create Table Authorities (Username Varchar (50) NOT NULL, authority VARCHAR (50) NOT NULL); CREATE UNIQUE INDEX ix_auth_username ON authorities (username, authority); ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users foreign key (username) REFERENCES users (username); 2) add access datasource your database and the Acegi jdbcDao, as follows: $ {jdbc.driverClassName} value> < / Property> $ {jdbc.url} value> proty> $ {jdbc.username} value> < / Property> $ {jdbc.password} value> p ROPERTY> bean> property> bean> 3) Add daoAuthenticationProvider: [
]/> proty> proty> bean> 5 value> print> bean> If you need to encrypt your password, add: print>, Acegi provides several encryption methods, detailed information, net.sf.acegisecurity.providers.Encoding4) Add AuthenticationManager: list> property> bean> 5) Add AccessDecisionManager: < bean id = "accessDecisionManager" class = "net.sf.acegisecurity.vote.AffirmativeBased"> false value> property> list> proty> bean> 6) Add authenticationProcessingFilterEntryPoint: /acegilogin.jsp value >
proty> false value> property> bean> where Acegilogin.jsp is the login page, a simplest login page as follows: <% @ Taglib prefix = 'c' uri = 'http://java.sun.com/jstl/core'%> <% @ page import = "Net.sf.acegisecurity.ui.abstractProcessingFilter"%> <% @ page import = "NET. sf.acegiseircurity.authenticationException "%> login title> head> login h1>