`

acegi 配置 2

阅读更多

实现自己的AuthenticationProcessingFilter:

package com.radicasys.lm.filter;



import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;



import org.acegisecurity.Authentication;

import org.acegisecurity.AuthenticationException;

import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;

import org.acegisecurity.ui.AbstractProcessingFilter;



public class MyAuthenticationProcessingFilter extends AbstractProcessingFilter {



    public static final String ACEGI_SECURITY_FORM_USERNAME_KEY = "j_username";

    public static final String ACEGI_SECURITY_FORM_PASSWORD_KEY = "j_password";

    public static final String ACEGI_SECURITY_LAST_USERNAME_KEY = "ACEGI_SECURITY_LAST_USERNAME";

    public static final String ACEGI_SECURITY_CURRENTUSERGROUP = "currenGroup";

    public static final String ACEGI_SECURITY_USERNAME_KEY = "ACEGI_SECURITY_USERNAME";

    //~ Methods ========================================================================================================



    public Authentication attemptAuthentication(HttpServletRequest request)

        throws AuthenticationException {

        String username = obtainUsername(request);

        String password = obtainPassword(request);

        String currentGroup = obtainCurrentGroup(request);

        if (username == null) {

            username = "";

        }



        if (password == null) {

            password = "";

        }

        request.getSession().setAttribute(ACEGI_SECURITY_USERNAME_KEY, username);

        username = username.trim()+"_"+currentGroup.trim();



        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);



        // Place the last username attempted into HttpSession for views

        request.getSession().setAttribute(ACEGI_SECURITY_LAST_USERNAME_KEY, username);



        //set currentGroup to HttpSession

        request.getSession().setAttribute(ACEGI_SECURITY_CURRENTUSERGROUP, currentGroup.trim());

        // Allow subclasses to set the "details" property

        setDetails(request, authRequest);



        return this.getAuthenticationManager().authenticate(authRequest);

    }



    /**

     * This filter by default responds to <code>/j_acegi_security_check</code>.

     *

     * @return the default

     */

    public String getDefaultFilterProcessesUrl() {

        return "/j_acegi_security_check";

    }



    public void init(FilterConfig filterConfig) throws ServletException {}



    /**

     * Enables subclasses to override the composition of the password, such as by including additional values

     * and a separator.<p>This might be used for example if a postcode/zipcode was required in addition to the

     * password. A delimiter such as a pipe (|) should be used to separate the password and extended value(s). The

     * <code>AuthenticationDao</code> will need to generate the expected password in a corresponding manner.</p>

     *

     * @param request so that request attributes can be retrieved

     *

     * @return the password that will be presented in the <code>Authentication</code> request token to the

     *         <code>AuthenticationManager</code>

     */

    protected String obtainPassword(HttpServletRequest request) {

        return request.getParameter(ACEGI_SECURITY_FORM_PASSWORD_KEY);

    }



    /**

     * Enables subclasses to override the composition of the username, such as by including additional values

     * and a separator.

     *

     * @param request so that request attributes can be retrieved

     *

     * @return the username that will be presented in the <code>Authentication</code> request token to the

     *         <code>AuthenticationManager</code>

     */

    protected String obtainUsername(HttpServletRequest request) {

        return request.getParameter(ACEGI_SECURITY_FORM_USERNAME_KEY);

    }



    protected String obtainCurrentGroup(HttpServletRequest request){

    	return request.getParameter(ACEGI_SECURITY_CURRENTUSERGROUP);

    }

    

    /**

     * Provided so that subclasses may configure what is put into the authentication request's details

     * property.

     *

     * @param request that an authentication request is being created for

     * @param authRequest the authentication request object that should have its details set

     */

    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {

        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));

    }



}

 在user model里面:

public class User implements UserDetails {
	private Set<Role> roles = new HashSet<Role>();

@Transient
	public GrantedAuthority[] getAuthorities() {
		return roles.toArray(new GrantedAuthority[0]);
	}

}

 在UserDaoHibernate里面:

@SuppressWarnings("unchecked")
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		List<User> users = getHibernateTemplate().find(
				"from User where email=?", username);
		if (users == null || users.isEmpty()) {
			throw new UsernameNotFoundException("user '" + username
					+ "' not found...");
		}
		else if(!users.get(0).isEnabled()){
			throw new DisabledException("user " + username
					+ " suspended...");
		}
		else {
			return (UserDetails) users.get(0);
		}
	}
 
分享到:
评论

相关推荐

    acegi配置文件

    acegi配置文件清单

    ldap与Acegi? ----Acegi配置文件解剖

    被解剖的acegi配置文件. 博文链接:https://rmn190.iteye.com/blog/175041

    Acegi配置web下载,完整

    Acegi配置.mhtAcegi配置的相关配置的信息在里面有一些

    Acegi配置指南[整理].pdf

    在 Acegi 配置指南中,我们主要关注如何设置和配置 Acegi 框架来保护 Web 应用程序的安全。 首先,我们需要在 `web.xml` 文件中配置 Acegi 的过滤器。在示例代码中,定义了一个名为 `Acegi Filter Chain Proxy` 的...

    Acegi 详细配置说明

    例如,为了配置认证管理器,我们可以创建一个`UserDetailsService`实现,然后在Acegi配置中引用它: ```xml &lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt; ...

    acegi

    2. **Acegi 的主要组件** - **AuthenticationManager**:处理用户的登录请求,负责身份验证。 - **AccessDecisionManager**:决定用户是否有权访问特定的资源。 - **SecurityContextHolder**:存储当前线程的安全...

    Acegi配置指南

    在配置Acegi时,首先需要在`web.xml`文件中定义一个名为`Acegi Filter Chain Proxy`的过滤器。这个过滤器是Acegi安全机制的核心,它负责拦截所有请求并根据配置执行相应的安全策略。下面是一段典型的`web.xml`配置...

    acegi实例,acegi详细配置和代码实现

    在本实例中,我们将探讨Acegi Security的配置、详细设置以及如何通过代码实现其功能。 首先,让我们理解Acegi Security的核心概念。该框架提供了一种基于角色的访问控制(RBAC)机制,允许开发人员定义用户权限并...

    Acegi 数据库配置安全策略 源代码及图解

    4. **XML配置转换**:在传统的Acegi配置中,安全规则通常写在XML配置文件中。这个资源可能包含了将这些静态XML配置转换为动态数据库配置的方法,这样可以更方便地根据用户角色和权限来调整安全策略。 5. **过滤器与...

    acegi的详细配置实现

    ### Acegi的详细配置实现 #### 一、整体架构概览 **Acegi Security** 是一个为Spring框架设计的安全管理工具,它提供了丰富的安全服务,包括认证(Authentication)、授权(Authorization)以及会话管理(Session ...

    基于java的ACEGI

    2. **配置Spring**:在Spring的配置文件中,定义Acegi的安全上下文,包括认证和授权策略。 3. **定义安全元数据**:创建XML配置或使用注解来指定哪些URL、方法需要进行安全控制。 4. **实现认证和授权逻辑**:根据...

    spring acegi 详细文档

    在Spring Acegi中,安全配置通常通过XML配置文件完成,但也可以使用注解进行简化。配置包括定义访问控制规则、配置认证和授权策略,以及设置安全过滤器链。 例如,以下是一个简单的XML配置示例,定义了一个URL访问...

    Acegi例子代码+一个很好的学习Acegi的网址

    2. **学习网址**:提供了一个适合初学者的基础教程,从零开始学习Acegi的用法和配置。这个网站可能包含了详细的教程、示例代码和常见问题解答,帮助你快速上手Acegi。 如果你在学习过程中遇到任何问题,可以通过...

    spring acegi 使用工程demo

    2. **授权**:Acegi的授权机制主要由`AccessDecisionManager`和`SecurityInterceptor`完成。`AccessDecisionManager`负责决定是否允许访问受保护的资源,而`SecurityInterceptor`则拦截请求并执行相应的安全策略。 ...

    CAS及客户端Acegi的安装配置指南

    2. 修改Spring配置文件,引入Acegi的安全拦截器(如)。 3. 配置Acegi的DaoAuthenticationProvider,使其与CAS服务器进行交互。这可能需要在Acegi的配置文件中添加如下的bean定义: ```xml ...

    acegi安全策略与CAS整合

    2. 配置CAS SERVER: 2.1 简单配置: 首先,要在CAS服务器上进行基础配置,包括设置服务器端点、服务验证URL等。 2.2 数据库验证配置: 如果需要使用数据库存储用户信息,需要配置数据库连接和验证逻辑。 2.4 ...

    ACEGI

    Acegi Security允许高度自定义的授权规则,可以基于角色、权限或复杂的业务逻辑进行配置。 Acegi Security的优势在于其灵活性和可扩展性,它不仅能够与Spring框架无缝集成,还能够适应各种各样的认证需求,无论是...

Global site tag (gtag.js) - Google Analytics