<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    為Spring Security添加IP限制功能

    項目中要為SpringSecurity添加IP限制功能,一開始的做法是繼承DaoAuthenticationProvider,在additionalAuthenticationChecks方法中使用WebAuthenticationDetails的getRemoteAddress獲取客戶端IP,然后判斷是否需要限制登錄。

    在tomcat上單獨部署時,這樣做一切正常,當使用apache作為前端代理時,發現總是提示IP錯誤,從日志中發現,getRemoteAddress方法總是返回apache的IP。查看WebAuthenticationDetails的源碼發現:

    this.remoteAddress = request.getRemoteAddr();

    當有代理存在時,request.getRemoteAddr()是不能正確獲取客戶端IP的(http://mrlee23.javaeye.com/blog/510747),

    需要使用http header中的x-forwarded-for獲取IP,方法如下:

    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
    public static final String getIpAddr(final HttpServletRequest request)
    throws Exception {
    if (request == null) {
    throw (new Exception(
    "getIpAddr method HttpServletRequest Object is null"));
    }
    String ipString = request.getHeader("x-forwarded-for");
    if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
    ipString = request.getHeader("Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString))request.getHeader("WL-Proxy-Client-IP");
    }
    if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString))request.getRemoteAddr();
    }

    // 多個路由時,取第一個非unknown的ip
    final String[] arr = ipString.split(",");
    for (final String str : arr) {
    if (!"unknown".equalsIgnoreCase(str))ipString = str;
    break;
    }
    }

    return ipString;
    }

    參考http://www.tkk7.com/taochen1984/articles/310072.html中的說明自定義Spring Security的一些組件:

    • 擴展WebAuthenticationDetails,保存request為一個屬性
    • 擴展WebAuthenticationDetailsSource,設置clazz為上面擴展出來的WebAuthenticationDetails修改spring security配置文件,添加以下bean:
         <bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
          <property name="authenticationDetailsSource" ref="authenticationDetailsSource" />
          <property name="authenticationManager" ref="authenticationManager" />
          <property name="filterProcessesUrl" value="/j_spring_security_check" />
          <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler" />
          <property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
          </bean>
          <bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
          <property name="defaultTargetUrl" value="/loginsuccess.jsp" />
          </bean>
          <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
          <property name="defaultFailureUrl" value="/admin.jsp?error=1" />
          </bean>
          <bean id="authenticationDetailsSource" class="javacommon.security.springsecurity.CustomWebAuthenticationDetailsSource"/>
          <bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
          <property name="loginFormUrl" value="/admin.jsp" />
          <property name="forceHttps" value="false" />
          </bean>

      取消s:http的auto-config屬性,將自定義的authenticationProcessingFilter設置為FORM_LOGIN_FILTER:

    <s:http access-denied-page="/fault.jsp"  entry-point-ref="authenticationProcessingFilterEntryPoint">
    <s:logout logout-success-url="/admin.jsp" />
    <s:remember-me/>
    <s:anonymous/>
    <s:custom-filter position="FORM_LOGIN_FILTER" ref="authenticationProcessingFilter" />
    </s:http>

    from: http://codingmu.ixiezi.com/

    posted on 2010-10-11 17:24 gembin 閱讀(3949) 評論(0)  編輯  收藏 所屬分類: Spring


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    free counters
    主站蜘蛛池模板: 成人免费在线观看网站| 亚洲国产一区在线观看| 久久大香伊焦在人线免费| 亚洲毛片免费观看| 久久久久久久岛国免费播放 | 亚洲国产成人精品激情| 在线观看的免费网站无遮挡| 国产成人亚洲合集青青草原精品| 亚洲AV无码乱码在线观看牲色| 国产精品亚洲精品久久精品| 亚洲日韩激情无码一区| 国内少妇偷人精品视频免费| 亚洲精品字幕在线观看| 毛片免费在线观看网站| 中文字幕一区二区免费| 亚洲另类自拍丝袜第五页| 亚洲av无码一区二区三区网站 | 久久精品九九亚洲精品天堂| 最近免费中文字幕中文高清| 亚洲国产成人久久综合一区| 中文字幕亚洲电影| 国产成人精品无码免费看| 美女黄网站人色视频免费| 亚洲国产成人久久| 久久亚洲高清观看| www国产亚洲精品久久久日本| 在线观看视频免费完整版| aa毛片免费全部播放完整| 久久亚洲精品无码VA大香大香| 国产人成免费视频网站| 亚洲精品亚洲人成在线| 亚洲永久永久永久永久永久精品| 亚洲VA综合VA国产产VA中| 欧美最猛性xxxxx免费| 四虎精品成人免费视频| 久久精品亚洲AV久久久无码 | 亚洲噜噜噜噜噜影院在线播放 | 一个人看的hd免费视频| 亚洲bt加勒比一区二区| 在线a人片天堂免费观看高清| 一本久久免费视频|