我有一个简单的身份验证提供程序,我正在尝试使用Spring Security。
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/**" access="isAuthenticated()" />
</security:http>
<security:authentication-manager>
<security:authentication-provider
ref="ipAddressAuthenticationProvider" />
</security:authentication-manager>
目前,通过上述配置,用户在第一次访问时被重定向到登录页面。我不想要这个重定向。我试图在每次访问页面时点击此身份验证提供程序。如何在不编写其他自定义代码的情况下完成此工作?
我猜我需要以某种方式干净地摆脱表单过滤器和基本过滤器。
结果
我使用下面的配置。我必须扩展AbstractPreAuthenticatedProcessingFilter
并简单地return "";
扩展它的两种抽象方法。
<security:http use-expressions="true" entry-point-ref="http403ForbiddenEntryPoint">
<security:intercept-url pattern="/**" access="isAuthenticated()" />
<security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthFilter" />
</security:http>
<bean id="preAuthFilter" class="com.hercules.ratinggame.business.security.IpAddressPreAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>
<bean id="http403ForbiddenEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint"/>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider
ref="ipAddressAuthenticationProvider" />
</security:authentication-manager>
答案 0 :(得分:3)
目前您使用的是auto-config="true"
,这意味着您获得了few filters configured iunder the hood,包括带有<form-login>
过滤器的UsernamePasswordAuthenticationFilter
元素。
此外,要在每次访问页面点击此身份验证提供程序,您需要一个可以从请求获取数据的过滤器(据我所知,IP地址)。过滤器可能会RequestHeaderAuthenticationFilter
或更有可能your own AbstractPreAuthenticatedProcessingFilter
implementation,可以访问您的身份验证管理器。
总而言之,配置如下:
<security:http use-expressions="true">
<security:intercept-url pattern="/**" access="isAuthenticated()" />
<security:logout /> <!-- optional -->
<security:custom-filter position="PRE_AUTH_FILTER"
ref="ipFromRequestPreAuthenticationFilter" />
</security:http>
<!-- this will probably extend AbstractPreAuthenticatedProcessingFilter -->
<bean id="ipFromRequestPreAuthenticationFilter"
class="com.example.IpFromRequestPreAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager" />
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="ipAddressAuthenticationProvider" />
</security:authentication-manager>