Spring 安全仅用于授权。外部认证

标签 spring security authentication authorization external

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

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

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

最佳答案

有很多相同的东西,只需要正确地谷歌。

无论如何,迄今为止我发现的最好的(几乎所有的 Spring 类(class))是 Krams这是基本的 Spring 安全性。

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

其他一些是:

  • Spring By Example
  • MK Young
  • And SpringSource Site itself

  • 编辑

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

    我的 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>
    

    现在如您所见,我指定了一个名为 loginService 的 bean作为我的身份验证提供程序,它是类 com.indyaah.service.LoginService 的 bean .

    相同的代码是:
    Pl 注意我截断了不必要的代码
    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");
            }
    
        }
    }
    

    请注意这里的 2 件事。
  • 我获取用户详细信息(在我的情况下来自 DB,您的可能有所不同。)并将其放在新的 org.springframework.security.core.userdetails.User 下。对象然后由方法返回到 Spring 安全性。
  • 还有权限(我根据我的 DB 架构从 DB 单独加载,您的情况可能会有所不同)并通过相同的 User 对象将其传递给 Spring Security。
  • 关于Spring 安全仅用于授权。外部认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12366763/

    相关文章:

    java - Spring Boot应用程序无法启动可能是由于依赖关系

    authentication - ServiceStack - UserAuth "Id"是 INT,而 RavenDb 预计 "Id"是 STRING

    java - 我的远程服务器 (Amazon EC2) 是否需要运行 2 个不同的服务器才能运行 EmberJS 和 Spring Boot?

    java - 使用 graphql-java-tools 和 graphql-spring-boot-starter 进行错误处理

    c# - 如何在 C# 中获取给定文件夹的组和用户名列表

    database - 在哪里定义安全角色?

    javascript - 通过JS获取用户个人信息

    java - "realm"在客户端的 http basic/digest 认证中有什么用?

    authentication - 使用 auth_request 模块和外部身份验证 API 的 Nginx 反向代理 - 错误 404

    java - org.hibernate.HibernateException : createSQLQuery is not valid without active transaction