DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

acegi的验证问题1 ---中文用户名登录,及md5加密密码方式

    在框架中使用了acegi,但是割接了一个微软的系统,系统中出现了中文用户名登录,这就造成了问题。
因为之前acegi都是另一个同事负责,现在同事不在,只能自己解决,找到acegi中取得用户名的地方
org.acegisecurity.ui.webapp.AuthenticationProcessingFilter 中的这段代码
 1public Authentication attemptAuthentication(HttpServletRequest request)
 2        throws AuthenticationException {
 3        String username = obtainUsername(request);
 4        String password = obtainPassword(request);
 5
 6        if (username == null{
 7            username = "";
 8        }

 9
10        if (password == null{
11            password = "";
12        }

13
14        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
15
16        // Place the last username attempted into HttpSession for views
17        request.getSession().setAttribute(ACEGI_SECURITY_LAST_USERNAME_KEY, username);
18
19        // Allow subclasses to set the "details" property
20        setDetails(request, authRequest);
21
22        return this.getAuthenticationManager().authenticate(authRequest);
23    }

24

取出username后发现是乱码,如果解决这个问题呢?第一个想到的是转码
username=new String(username.getBytes("ISO8859-1"),"UTF-8");
解决问题,但是这段代码要嵌入到acegi中必须重新编译acegi
上边的办法改动太大,再想办法,想到既然问题是来自编码,看看web.xml的filter发现原因在这
spring的filter是解决编码问题的,但是因为acegi的filter在spring之前,所以编码没有转码。又不能把acegi的filter挪到spring filter之后,这样就有安全问题了。

那就增加一个filter,只过滤登录链接,然后设置一下代替spring的encodingfilter设置一下编码,解决问题
public class EncodeChnUsernameFilter implements Filter{
    
private static final String ACEGI_SECURITY_FORM_USERNAME_KEY ="j_username";
    
private static final String ACEGI_SECURITY_FORM_PASSWORD_KEY = "j_password";
    
public void destroy() {
        
    }


    
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(
"UTF-8");
        chain.doFilter(request, response);
    }


    
public void init(FilterConfig arg0) throws ServletException {
        
    }

}
中文用户名登录问题解决了。

但是另一个问题来了,密码是非明文的md5加密的,需要加密,同样不想更改acegi。
那好吧继续使用filter,看看能否getParameter后再set回去
String password= request.getParameter("j_password");
//这里是个md5加密函数
password = md5(password);
//怎么set进去呢?
request.getParameterMap().put("j_password",password);
//启动试一下,异常报错,map不能put,看一下异常,发现这个不是普通的map,是org.apache.catalina.util.ParameterMap,这个map中有个标志位lock,tomcat不让更改http接收到的值。
//基于不服输的精神,一定要搞定它,呵呵
ParameterMap map = (ParameterMap)request.getParameterMap();
map.setLock(
false);
map.put(
"j_password",password);
map.setLock(
true);
//搞定?不对,编译不通过,发现org.apache.catalina.util.ParameterMap的jar包是catalina.jar。
把这个包放到lib下编译,通过,运行出向下转型错误,仔细看一下发现request.getParameterMap()出来的ParameterMap.getClass()的id是300多,而接受转型
的ParameterMap.class.getClass()是6000多,不是一个类啊。想想也对,lib下和tomcat的server/lib下各有一个catalina.jar这个就是两个类了。

eclipse add 外部jar包,直接add上tomcat中的catalina.jar,编译运行,还是有问题,这次是报的classNotFound异常,为什么会这样呢,命名Server
/lib下有这个jar包,后来想了下明白了。tomcat一定是限定了catalina.jar不能被普通的用户类所直接引用。处于安全性考虑吧。这条路走不通了,回到acegi才发现acegi的配置文件中是可以随意配置autheticationfilter的,自己写一个autheticationfilter,配置进去,不用系统的,解决问题,filter里边怎么写都没问题,爱怎么处理怎么处理,呵呵

posted on 2008-09-19 21:29 dreamstone 阅读(3694) 评论(0)  编辑  收藏 所属分类: 其它开源框架


只有注册用户登录后才能发表评论。


网站导航: