Spring Security仅用于授权。外部认证

2023-12-31

正如标题所述,我正在开发一个 Web 应用程序,该应用程序从外部应用程序接收用户身份验证信息。我的应用程序的弹簧控制器获取用户信息并将其存储在会话中。我想在 Spring Security 中对这个用户进行身份验证,然后使用他的角色授予/拒绝对 url 的访问,例如

<intercept-url pattern="/myprotectedpage*" access="hasRole('rightrole')" />

我读了一些关于 PRE_AUTH_FILTER 和 UserDetailsS​​ervice 的教程,但我无法明白这一点。 Spring Security 的应用程序生命周期是怎样的?涉及哪些类? 我需要一些完整的工作样本。


那里有很多相同的东西,只需要正确谷歌搜索即可。

无论如何,迄今为止我发现的最好的(几乎所有春季图)是Krams http://krams915.blogspot.com这是基本的 Spring 安全性。

http://krams915.blogspot.com/2010/12/spring-security-mvc-integration_18.html http://krams915.blogspot.com/2010/12/spring-security-mvc-integration_18.html

要实现 UserDetailService,这里是链接

http://krams915.blogspot.in/2012/01/spring-security-31-implement_5023.html http://krams915.blogspot.in/2012/01/spring-security-31-implement_5023.html

其他一些是:

  1. 春天的例子 http://www.springbyexample.org/examples/simple-spring-security-webapp.html
  2. MK Young http://www.mkyong.com/tutorials/spring-security-tutorials/
  3. 以及 SpringSource 站点本身 http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity.html

EDIT

这就是我自己的应用程序进行身份验证的方式(请注意,我不使用外部身份验证,我只是从数据库获取详细信息,但我想这应该不是什么大问题)。

My security-context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled">
    </global-method-security>

    <http use-expressions="true">
        <intercept-url pattern="/favicon.ico" access="permitAll" />
        <intercept-url pattern="/static/**" access="permitAll"/>
        <intercept-url pattern="/login.jsp*" access="permitAll"/> 
        <intercept-url pattern="/Admin/**" access="hasAnyRole('ROLE_SUPER_USER')"/>
        <intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_SUPER_USER','ROLE_ADMIN'"/>
        <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" />
        <http-basic/>
        <logout logout-success-url="/login.jsp"/>
        <remember-me user-service-ref="loginService" /
     </http>

    <authentication-manager>
        <authentication-provider user-service-ref="loginService">
         <password-encoder hash="md5"/>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="loginService" class="com.indyaah.service.LoginService">
    </beans:bean>
    <beans:bean id="authService" class="com.indyaah.service.AuthService" />
</beans:beans>

现在如你所见,我已经指定了一个名为的 beanloginService作为我的身份验证提供者,它是类的 beancom.indyaah.service.LoginService.

相同的代码是:请注意我已经截断了不必要的代码

package com.indyaah.service;
..
@Service
public class LoginService implements UserDetailsService {

....

    /**
     * Implementation for custom spring security UserDetailsService
     */
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
        logger.debug("Inside get member by username");
        if (userName != null) {
            Member memberVO = memberMapper.getMemberByUsername(userName);
            if (memberVO != null) {
                ArrayList<String> authList = memberRolesMapper.getMemberRoles(memberVO.getMemberId());

                List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
                for (String role : authList) {
                    System.out.println(role);
                    authorities.add(new GrantedAuthorityImpl(role.toString()));
                }

                if (memberVO.getEnabled()) {
                    User user = new User(memberVO.getUserName(), memberVO.getPassword(), true, true, true, true, authorities);
                    return user;
                } else {
                    logger.error("User with login: " + userName + " not Enabled in database. Authentication failed for user ");
                    throw new UsernameNotFoundException("User Not Enabled");
                }
            } else {
                logger.error("User with login: " + userName + " not found in database. Authentication failed for user ");
                throw new UsernameNotFoundException("user not found in database");
            }
        } else {
            logger.error("No User specified in the login ");
            throw new UsernameNotFoundException("No username specified");
        }

    }
}

这里注意两件事。

  1. 我获取用户详细信息(在我的情况下来自数据库,您的可能有所不同)并将其放在新的org.springframework.security.core.userdetails.User然后该方法将对象返回给 spring security。
  2. 此外,当局(我根据我的数据库架构与数据库分开加载,您的场景可能会有所不同)并通过相同的 User 对象将其传递给 spring security。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security仅用于授权。外部认证 的相关文章

随机推荐

  • 为什么装饰器是一种结构性设计模式而不是行为性设计模式?

    我认为自己拥有 GoF 设计模式的中级知识 然而 当我将这些模式分类为结构模式和行为模式时 我感到很困惑 我对创造模式没有任何困惑 来自维基百科 装饰器模式 在面向对象编程中 装饰器模式是一种允许behavior静态或动态地添加到单个对象
  • Haskell有foldlM'吗?

    如何严格折叠 monad Data Foldable http www haskell org ghc docs 6 12 2 html libraries base 4 2 0 1 Data Foldable html有严格的foldl
  • REBOL 中的 If...else if...else

    我注意到 REBOL 没有内置if elsif else语法 就像这样 theVar 60 This won t work if theVar gt 60 print Greater than 60 elsif theVar 3 print
  • 如何仅使用 CSS 制作网格(如方格纸网格)?

    如何仅使用 CSS 制作网格 如方格纸网格 我只想仅使用 CSS 制作一张虚拟网格纸 要制作网格 您可以使用 CSS 渐变 它适用于所有现代浏览器 参见卡尼乌斯 http caniuse com search linear gradient
  • 如何调用自定义函数作为 jQuery 函数链的一部分

    这似乎是一件很简单的事情 但我找不到jQuery函数来处理这个问题 例如 div show sayHi function sayHi obj obj html hi sayHi 不是一个jQuery函数 因此不能在此上下文中调用它 你需要这
  • 不使用 Javascript 从 Google 地图获取路线和方向?

    因此 如果您正在编写一个应用程序 而不是网站 并且需要解析从一个位置到另一个位置的路线 那么如何在不使用 Javascript 的情况下从 Google 地图获取信息呢 我想知道 Google 地图上是否有任何一组特殊的 URL 或 API
  • 找不到盒子“laravel/homestead”

    我已经下载了laravel homestead手动装箱here https atlas hashicorp com laravel boxes homestead 我成功添加了盒子 vagrant box add file path to
  • 在 Bash 中,有没有办法在双引号中将变量展开两次?

    为了调试我的脚本 我想在每个输出的开头添加内部变量 FUNCNAME 和 LINENO 这样我就知道输出发生在哪个函数和行号上 foo local bar something echo FUNCNAME LINENO I just set
  • Pyinstaller 设置图标不改变

    当我使用命令时 pyinstaller exe icon test ico F noconsole test py 所有图标均不会更改为 test ico 一些图标保留为 pyinstaller 的默认图标 Why 所有图标均更改为 视窗
  • 使用字母间距填充div

    我遇到的问题是填写div与文本使用letter spacing 主要问题是我不知道宽度div 首先我想使用 text align justify 但从那以后我一直在黑暗中奔跑 不知道如何解决这个问题 我猜想一些脚本魔法可能会解决这个问题 一
  • 冲突的类型和先前的 x 声明在这里......什么?

    当我有时间的时候 我已经自学了几个月的 C 语言 但我遇到了一个我不知道如何解决的问题 具体来说 当我尝试使用 gcc 编译它时 我得到 geometry c 8 error conflicting types for trapezoid
  • 来自带有时区和夏令时的字符串的 Qt QDateTime

    我正在从字符串插入时间 QDateTime time QDateTime fromString Wed Mar 26 22 37 40 2019 GMT 08 qDebug lt
  • 从通知区域发出的卡通语音气泡叫什么?如何创建一个?

    谁能告诉我以下弹出窗口的名称是什么 如何为我的应用程序创建这样的弹出窗口 To be more specific this is indeed called a Notification http msdn microsoft com en
  • Clojure中如何加载程序资源

    如何在 Clojure 程序中加载图标 字符串 图形元素 脚本等程序资源 我使用的项目布局类似于许多 Java 项目中的布局 其中有一个 资源 目录挂在 源 目录下 jar 文件是从源代码创建的并包含资源 但我似乎无法像在 Java 中那样
  • 将 JWK json 转换为公钥 golang (lestrrat-go)

    我使用 JWKS 格式从身份验证服务提供公钥 该公钥可用于验证来自该身份验证服务的令牌 但是 要执行验证 我需要从 JWK 重建公钥 我该如何转换它 type JWKeys struct Keys JWKey json keys type
  • 多选到数组

  • 使用 SharePoint 客户端对象模型检查列表列是否存在?

    使用 SharePoint 2010 中的客户端对象模型 C 如何确定给定列表中是否存在指定的列 字段 名称 谢谢 魔术安迪 刚刚在搜索相同的东西时发现了这个 但看起来 Sharepoint 2010 有内置的东西 至少对于服务器模型 li
  • 图像未通过 android webview 加载

    我有一个加载网页的网络视图 有时该网页中有图片 但是 我遇到了 2 个图像无法加载的情况 并且每个情况都给出了不同的结果 结果 1 网页已加载 但图像未加载 使用的格式 jpeg 结果 2 网页已加载 但图像未加载 然而 在该图像所在的位置
  • 转义保留字

    Sitecore 提供了一种转义 Sitecore 查询中包含不喜欢字符的单词的方法 此类字符包括连字符和空格 为了简化我的生活 我编写了一个简单的辅助函数 可以转义 Sitecore 查询的每个部分 并且它运行良好一段时间 public
  • Spring Security仅用于授权。外部认证

    正如标题所述 我正在开发一个 Web 应用程序 该应用程序从外部应用程序接收用户身份验证信息 我的应用程序的弹簧控制器获取用户信息并将其存储在会话中 我想在 Spring Security 中对这个用户进行身份验证 然后使用他的角色授予 拒