在框架中使用了acegi,但是割接了一個(gè)微軟的系統(tǒng),系統(tǒng)中出現(xiàn)了中文用戶名登錄,這就造成了問(wèn)題。
因?yàn)橹癮cegi都是另一個(gè)同事負(fù)責(zé),現(xiàn)在同事不在,只能自己解決,找到acegi中取得用戶名的地方
org.acegisecurity.ui.webapp.AuthenticationProcessingFilter 中的這段代碼
1
public 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后發(fā)現(xiàn)是亂碼,如果解決這個(gè)問(wèn)題呢?第一個(gè)想到的是轉(zhuǎn)碼
username=new String(username.getBytes("ISO8859-1"),"UTF-8");
解決問(wèn)題,但是這段代碼要嵌入到acegi中必須重新編譯acegi
上邊的辦法改動(dòng)太大,再想辦法,想到既然問(wèn)題是來(lái)自編碼,看看web.xml的filter發(fā)現(xiàn)原因在這
spring的filter是解決編碼問(wèn)題的,但是因?yàn)閍cegi的filter在spring之前,所以編碼沒(méi)有轉(zhuǎn)碼。又不能把a(bǔ)cegi的filter挪到spring filter之后,這樣就有安全問(wèn)題了。
那就增加一個(gè)filter,只過(guò)濾登錄鏈接,然后設(shè)置一下代替spring的encodingfilter設(shè)置一下編碼,解決問(wèn)題

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
{
}
}
中文用戶名登錄問(wèn)題解決了。
但是另一個(gè)問(wèn)題來(lái)了,密碼是非明文的md5加密的,需要加密,同樣不想更改acegi。
那好吧繼續(xù)使用filter,看看能否getParameter后再set回去
String password= request.getParameter("j_password");
//這里是個(gè)md5加密函數(shù)
password = md5(password);
//怎么set進(jìn)去呢?
request.getParameterMap().put("j_password",password);
//啟動(dòng)試一下,異常報(bào)錯(cuò),map不能put,看一下異常,發(fā)現(xiàn)這個(gè)不是普通的map,是org.apache.catalina.util.ParameterMap,這個(gè)map中有個(gè)標(biāo)志位lock,tomcat不讓更改http接收到的值。
//基于不服輸?shù)木瘢欢ㄒ愣ㄋ呛?/span>
ParameterMap map = (ParameterMap)request.getParameterMap();
map.setLock(false);
map.put("j_password",password);
map.setLock(true);
//搞定?不對(duì),編譯不通過(guò),發(fā)現(xiàn)org.apache.catalina.util.ParameterMap的jar包是catalina.jar。
把這個(gè)包放到lib下編譯,通過(guò),運(yùn)行出向下轉(zhuǎn)型錯(cuò)誤,仔細(xì)看一下發(fā)現(xiàn)request.getParameterMap()出來(lái)的ParameterMap.getClass()的id是300多,而接受轉(zhuǎn)型
的ParameterMap.class.getClass()是6000多,不是一個(gè)類啊。想想也對(duì),lib下和tomcat的server/lib下各有一個(gè)catalina.jar這個(gè)就是兩個(gè)類了。
eclipse add 外部jar包,直接add上tomcat中的catalina.jar,編譯運(yùn)行,還是有問(wèn)題,這次是報(bào)的classNotFound異常,為什么會(huì)這樣呢,命名Server/lib下有這個(gè)jar包,后來(lái)想了下明白了。tomcat一定是限定了catalina.jar不能被普通的用戶類所直接引用。處于安全性考慮吧。這條路走不通了,回到acegi才發(fā)現(xiàn)acegi的配置文件中是可以隨意配置autheticationfilter的,自己寫一個(gè)autheticationfilter,配置進(jìn)去,不用系統(tǒng)的,解決問(wèn)題,filter里邊怎么寫都沒(méi)問(wèn)題,愛怎么處理怎么處理,呵呵