16

Spring Security を使用して、ユーザーの Web アプリへのアクセスを IP でフィルター処理する方法を知りたいです。私は拡張するAbstractAuthenticationProcessingFilterか、そのようなものを独自の方法でメソッドをオーバーライドする必要がありますか? もしそうなら、そのような拡張の例と でのフィルタ記述の例を挙げていただけweb.xmlますか? 前もって感謝します。

PS 私のアプリでは、Spring Security のサポート ( default を使用org.springframework.web.filter.DelegatingFilterProxy) もありますが、ユーザーの資格情報だけでなく、IP も確認したいと考えています。

4

2 に答える 2

15

これを行う 1 つの方法は、Spring Security のWeb Security Expressionsを使用することです。例えば:

<http use-expressions="true">
    <intercept-url pattern="/admin*"
        access="hasRole('admin') and hasIpAddress('192.168.1.0/24')"/>
    ...
</http>
于 2012-10-08T17:06:14.100 に答える
1

Anshu の回答は、ユーザーを IP で認証するという良い考えですが、cas 認証では機能しない可能性があります。別の解決策があります。この状況にはフィルターを使用する方が適しています。

public class IPAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    private AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService;
    private static Set<String> ipWhitelist;

    @Autowired
    private AppProperty appProperty;

    @PostConstruct
    public void init() {
        ipWhitelist = new HashSet<>(Arrays.asList(appProperty.getIpWhitelist()));
        setAuthenticationSuccessHandler(new AuthenticationSuccessHandler() {
            @Override
            public void onAuthenticationSuccess(
                    HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse,
                    Authentication authentication) throws IOException, ServletException {
                // do nothing
            }
        });
    }

    public IPAuthenticationFilter() {
        super("/");
    }

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException, IOException {
        String userName = request.getHeader(appProperty.getHeaderCurUser());
        Assertion assertion = new AssertionImpl(userName);
        CasAssertionAuthenticationToken token = new CasAssertionAuthenticationToken(assertion, "");
        UserDetails userDetails = authenticationUserDetailsService.loadUserDetails(token);

        CasAuthenticationToken result = new CasAuthenticationToken(
                "an-id-for-ip-auth",
                userDetails,
                request.getRemoteAddr(),
                userDetails.getAuthorities(),
                userDetails,
                assertion
        );
        return result;
    }

    protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getHeader(appProperty.getHeaderCurUser());
        return ipWhitelist.contains(request.getRemoteAddr()) && !StringUtils.isEmpty(userName);
    }

    protected void successfulAuthentication(
            HttpServletRequest request,
            HttpServletResponse response,
            FilterChain chain,
            Authentication authResult) throws IOException, ServletException {
        super.successfulAuthentication(request, response, chain, authResult);
        chain.doFilter(request, response);
    }

    public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> getAuthenticationUserDetailsService() {
        return authenticationUserDetailsService;
    }

    public void setAuthenticationUserDetailsService(
            AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService) {
        this.authenticationUserDetailsService = authenticationUserDetailsService;
    }
}

このフィルターは、次のように cas の前に追加できます。

http.addFilterBefore(ipAuthenticationFilter(), CasAuthenticationFilter.class)
于 2017-12-26T10:02:57.557 に答える