hjg
2023-11-22 975c04c0181b3da82a79cb04abe6a2f1799eb363
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.mandi.servlet;
 
import java.io.IOException;
import java.util.UUID;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.log4j.Logger;
@WebFilter(filterName="tokenfilter",urlPatterns={"*.jsf","*.htm"})
public class Tokenfilter implements Filter {
    private Logger log=Logger.getLogger(Tokenfilter.class);
    String token_s_name;
    String token_c_name;
    @Override
    public void destroy() {
        
    }
 
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest r=(HttpServletRequest)arg0;
        HttpSession s=r.getSession();
        Object token_s=s.getAttribute(token_s_name);
//        log.info("token_s:"+token_s);
        if(token_s==null)
        {
            s.setAttribute(token_s_name, UUID.randomUUID().toString());
            arg2.doFilter(arg0, arg1);
            return;
        }
        String token_c=r.getParameter(token_c_name);
        if(token_c==null)
        {
            arg2.doFilter(arg0, arg1);
            return;
        }
        s.setAttribute(token_s_name, UUID.randomUUID().toString());
        if(token_c.equals(token_s))
        {
            arg2.doFilter(arg0, arg1);
            return;
        }
    }
 
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        token_s_name="stoken";
        token_c_name="ctoken";
    }
 
}