自定义 Spring 3.0 安全过滤器、多个入口点、AuthenticationProvider

2023-12-31

我需要我的安全性具有以下逻辑:

  1. 检查标头参数是否存在
  2. 根据参数的存在,重定向到登录页面(如果未经过身份验证),或检查基本身份验证令牌

在这两种情况下,我都有相同的身份验证提供程序,但我无法让它工作。 委托入口点工作正常,但我从未进入我的自定义身份验证提供程序......

这是我的安全配置:

    <security:global-method-security
    secured-annotations="enabled" />

<security:http entry-point-ref="delegatingAuthenticationEntryPoint"
    use-expressions="true" auto-config="false">
    <!-- <security:custom-filter position="FORM_LOGIN_FILTER" -->
    <!-- ref="usernamePasswordAuthenticationFilter" /> -->
    <!-- <security:custom-filter position="BASIC_AUTH_FILTER" -->
    <!-- ref="basicAuthenticationFilter" /> -->
    <security:intercept-url pattern="/login*"
        filters="none" />
    <security:intercept-url pattern="/portimaLogin*"
        filters="none" />
    <security:intercept-url pattern="/**"
        access="isAuthenticated()" />
</security:http>

<bean id="delegatingAuthenticationEntryPoint"
    class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint">
    <constructor-arg>
        <map>
            <entry key="hasHeader('portima','true')" value-ref="PortimaLoginUrlAuthenticationEntryPoint" />
        </map>
    </constructor-arg>
    <property name="defaultEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="usernamePasswordAuthenticationFilter"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
</bean>

<bean id="basicAuthenticationFilter"
    class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="PortimaLoginUrlAuthenticationEntryPoint"
    class="be.ap.common.security.spring.PortimaLoginUrlAuthenticationEntryPoint">
    <property name="loginFormUrl" value="${portima.login.page}" />
</bean>

<bean id="authenticationEntryPoint"
    class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
    <property name="realmName" value="AP" />
</bean>

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider
        ref="authenticationProvider" />
</security:authentication-manager>

<bean id="authenticationProvider" class="be.ap.common.security.spring.APAuthenticationProvider" />

<bean id="userDetailsService" class="be.ap.common.security.spring.APUserDetailsService" />

任何想法 ?


我终于可以工作了。

这是我的上下文文件:

    <security:http entry-point-ref="delegatingAuthenticationEntryPoint"
    use-expressions="true">
    <security:custom-filter position="PRE_AUTH_FILTER"
        ref="preAuthenticationFilter" />
    <security:custom-filter position="FORM_LOGIN_FILTER"
        ref="usernamePasswordAuthenticationFilter" />
    <security:custom-filter position="BASIC_AUTH_FILTER"
        ref="basicAuthenticationFilter" />
    <security:intercept-url pattern="/login*"
        filters="none" />
    <security:intercept-url pattern="/portimaLogin*"
        filters="none" />
    <security:intercept-url pattern="/accessDenied*"
        filters="none" />
    <security:intercept-url pattern="/**"
        access="isAuthenticated()" />
    <security:access-denied-handler ref="accessDeniedHandler" />
</security:http>

<!-- Spring Security Custom Filters -->

<bean id="usernamePasswordAuthenticationFilter"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
</bean>

<bean id="basicAuthenticationFilter"
    class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="preAuthenticationFilter" class="be.ap.common.security.spring.APPreAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
</bean>

<!-- Spring Security Custom EntryPoint -->

<bean id="delegatingAuthenticationEntryPoint"
    class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint">
    <constructor-arg>
        <map>
            <entry key="hasHeader('portima','true')" value-ref="PortimaLoginUrlAuthenticationEntryPoint" />
        </map>
    </constructor-arg>
    <property name="defaultEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="PortimaLoginUrlAuthenticationEntryPoint"
    class="be.ap.common.security.spring.PortimaLoginUrlAuthenticationEntryPoint">
    <property name="loginFormUrl" value="${portima.login.page}" />
</bean>

<bean id="authenticationEntryPoint"
    class="be.ap.common.security.spring.APBasicAuthenticationEntryPoint">
    <property name="realmName" value="AP" />
</bean>
<bean id="accessDeniedHandler"
    class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
    <property name="errorPage" value="/accessDenied" />
</bean>

<bean id="authenticationFailureHandler"
    class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
    <property name="exceptionMappings">
        <props>
            <prop
                key="org.springframework.security.authentication.BadCredentialsException">
                /accessDenied
            </prop>
            <prop
                key="org.springframework.security.authentication.CredentialsExpiredException">
                /accessDenied
            </prop>
            <prop key="org.springframework.security.authentication.LockedException">
                /accessDenied
            </prop>
            <prop
                key="org.springframework.security.authentication.DisabledException">
                /accessDenied
            </prop>
        </props>
    </property>
</bean>

<!-- Spring Security Authentication Manager -->

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider
        ref="authenticationProvider" />
</security:authentication-manager>

<bean id="authenticationProvider" class="be.ap.common.security.spring.APAuthenticationProvider" />

<bean id="userDetailsService" class="be.ap.common.security.spring.APUserDetailsService" />

<!-- for Mock -->
<bean id="SSOService" class="be.ap.security.service.SSOServiceMockImpl" />

正如你所看到的,我也添加了一些东西。

为了解决这个问题,我删除了自动配置属性,取消注释过滤器,并正确定义它们。

对于其他想要快速了解其用途的人,以下是流程:

  1. PRE_AUTH_FILTER 将检查类似 SSO 的服务来预填充身份验证对象(如果已在 SSO 中进行身份验证)
  2. 然后 delegatingAuthenticationEntryPoint 将根据请求标头选择如何进行身份验证
  3. The two ways are :
    • 自定义 LoginUrlAuthenticationEntryPoint
    • 自定义BasicAuthenticationEntryPoint

当 PreAuth 使用我的 SSO 服务时,BasicAuth 和 LoginURLAuth 使用相同的 AuthenticationProvider。

希望它对其他人有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义 Spring 3.0 安全过滤器、多个入口点、AuthenticationProvider 的相关文章

随机推荐

  • 哪些 Rails 插件是好的、稳定的并且*真正*增强了您的代码?

    任何人都有一个 Rails 插件列表 它们都是stable并为您提供足够的功能值得付出额外的支持 Edit 我最感兴趣的是最好 最完整的插件列表 这样我就可以在下次启动 Rails 应用程序时使用它 我目前不需要特定的插件 您可以使用bor
  • 如何计算C#应用程序的执行时间

    如何计算c 应用程序的执行时间 我有 C Windows 应用程序 我需要在其中计算执行时间 但我不知道必须在哪里进行此操作 谁能帮帮我吗 使用 System Diagnostics 的秒表 static void Main string
  • 在 iOS 7 中阻止传入短信

    我想为越狱设备编写一个调整 阻止来自电话号码的消息 在 iOS 7 中 首先我使用了 creker 的第二个答案这个链接 https stackoverflow com questions 16219799 block sms on ios
  • React Native AsyncStorage:无法解析 getItem 返回的承诺

    我有以下代码应该从 AsyncStorage 返回一个项目 然而 该项目从未被阅读 const key shoppingListItems export default class ShoppingListService static as
  • 从 python 3.x 写入 excel 2003 文件

    我有一个非常大的 CSV 数据集 几百万条记录 我已经过滤和调整了这个列表 并将其拆分为客户规范 这一切都是在Python3 3中完成的 最后一个要求是将这些拆分列表保存为 Excel 格式 他们有一个实用程序 可以在进行一些计算并检查数据
  • 将 super init 赋值给 self 有什么作用?

    鉴于这段代码 id init self super init if self nil return self 我知道我们正在检查超级初始化是否没有失败 但否则对我来说毫无意义 我希望得到一个菜鸟的解释 为什么我要把 super init 的
  • PowerShell 重命名文件名并保留扩展名

    我在这里找到了代码 它可以满足我的需要 files gci filter txt select fullname foreach file in files filename file fullname newFilename filena
  • Chrome 扩展后台页面中的 Firebase 身份验证

    如何在 Chrome 扩展程序中使用 Firebase 进行身份验证 我需要在 Forge 中指定允许的域列表 扩展程序的 Chrome 域只是一个类似散列的大字符串 我确实读过这个 authClient login 问题 https st
  • Caffe 中的“lr_policy”是什么?

    我只是想知道如何使用Caffe http caffe berkeleyvision org 为此 我只是看看不同的 prototxt示例文件夹中的文件 有一个选项我不明白 The learning rate policy lr policy
  • 如何在Rails中使用mini_magick解码base64图像文件?

    在我们的 Rails 4 应用程序中 图像以 base64 字符串上传到服务器 uploaded io data image jpeg base64 9j 4AAQSkZJRgABAQAAAQABAAD 2 我们想要检索内容类型 大小等并将
  • Cabal 多个可执行文件

    我正在使用 Yesod 开发一个网站 我正在运行正常的构建 但我似乎无法可靠地填充我的数据库 我有第二个 haskell 程序来填充数据库 并将其添加到我的 cabal 文件中 如下所示 executable program if flag
  • 在带有 Doctrine2 的 Symfony2 上,Object = Entity 吗?

    假设我有一个User class user new User 1 user gt setName Bob save bob to database with ID 1 user gt setGender Male save male to
  • 有没有任何有用的工具可以诊断 Qt 布局和间距问题?

    另请参阅相关问题 如何调试 Qt 布局问题 https stackoverflow com questions 232267 how do you debug qt layout problems 我有一些复杂的小部件层次结构 我正在尝试布
  • 如何实现碰撞检测?

    from graphics import import time import random def main numx random randint 10 700 wn GraphWin AK 700 700 wn setBackgrou
  • 理解Rails 3的respond_with

    利用 ActionController 的新功能respond with方法 它如何确定当操作 保存 成功和不成功时渲染什么 我问这个问题是因为我试图让脚手架生成的规范 包括在下面 通过 如果只是为了让我能够理解它 该应用程序工作正常 但奇
  • 从 MsBuild 定义 C# 预处理器

    In my C 文件我想要这样的预处理器条件 if DEMO ShowSplash endif 我正在从命令行运行此命令 MSBuild MySolution sln p Configuration Release p Platform A
  • Xamarin.Forms 自定义 Android NavigationPageRenderer 标题和副标题

    目前正在开发一个项目 我想使用 AppCompat 并在大多数页面上设置标题和副标题 使用 AppCompat 根本不起作用 既不设置属性也不使用自定义视图 当不使用 AppCompat 时 两者都会按预期工作 完整的源代码可用here h
  • 当我使用 ax.set_yscale('log') 时如何摆脱原来的 yticks/yticklabels

    我正在符号学图中绘制一些值 我需要使用 ax set yscale log 而不是 ax semilogy 因为我有一些带有负值的数组 所以我可以将比例设置为符号日志 My code ax plt subplot 132 ax set ys
  • 无法在 Spring Boot 中创建切面

    我只想实现一件简单的事情 创建日志记录方面 它应该挂钩每个方法以便能够打印参数 该方面如下所示 Aspect Component Slf4j public class MyLogger public MyLogger AfterReturn
  • 自定义 Spring 3.0 安全过滤器、多个入口点、AuthenticationProvider

    我需要我的安全性具有以下逻辑 检查标头参数是否存在 根据参数的存在 重定向到登录页面 如果未经过身份验证 或检查基本身份验证令牌 在这两种情况下 我都有相同的身份验证提供程序 但我无法让它工作 委托入口点工作正常 但我从未进入我的自定义身份