繁体   English   中英

将 Spring Security 插入项目的最佳方法是什么? 我有两种不同的方法来做到这一点

[英]What is the best way to insert Spring Security into a project? I have 2 differents way to do it

我是 Spring Security 的新手,我对我在 2 个不同项目中发现的这 2 个不同配置有一些疑问。 我想了解一个比另一个更好,或者它们是否等效。

项目 1:

项目1的spring-security.xml

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

    <security:http>
        <security:intercept-url pattern="/springLogin" access="permitAll"/>
        <security:intercept-url pattern="/doSpringLogin" access="permitAll"/>
        <security:intercept-url pattern="/springHome" access="hasRole('ROLE_USER')"/>
        <security:intercept-url pattern="/springLogout" access="permitAll"/>
        <security:intercept-url pattern="/springLogin?error=true" access="permitAll"/>
        <security:form-login login-page="/springLogin" login-processing-url="/doSpringLogin"
        default-target-url="/springHome" authentication-failure-url="/springLogin?error=true"
        username-parameter="username" password-parameter="password"
        />
        <security:csrf disabled="true"/>
        <security:logout logout-url="/springLogout" logout-success-url="/springLogin"/>
    </security:http>

    <bean id="userDetailsServiceImpl" class="com.demo.security.UserDetailsServiceImpl"></bean>

    <bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsServiceImpl"></property>
    </bean>

    <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
        <constructor-arg name="providers">
            <list>
                <ref bean="authenticationProvider"/>
            </list>
        </constructor-arg>
    </bean>

    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userDetailsServiceImpl">
            <security:password-encoder hash="plaintext"></security:password-encoder>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

在之前的 Spring Security 配置文件中可以看到,我首先声明了受保护的资源和这些资源的访问权限(什么样的用户可以访问这些资源)

然后声明了一些bean,它们是:

1) userDetailsS​​erviceImplcom.demo.security.UserDetailsS​​erviceImpl类的一个实例:

public class UserDetailsServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println(username);

        // Obtain the User object from the User database table using the username as key:
        User user = RegisteryDAO.getUserDAO().getUserByUsername(username);

        if(user == null){
            return null;
        }

        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        // Populate the authorites list with a new SimpleGrantedAuthority object created using the user role:
        authorities.add(new SimpleGrantedAuthority(user.getRole()));

        // Create a new UserDetail object using the username and its authorities list:
        UserDetails userDetails = new org.springframework.security.core.userdetails.
                User(user.getUsername(), user.getPassword(), true, true, true, true, authorities);


        return userDetails;
    }

}

如您所见,此 bean 是 Spring 提供的UserDetailsS​​ervice接口的实现。 所以它做了以下操作:

  • 使用用户名作为键从用户数据库表中获取用户对象。

  • 使用用户角色创建的新SimpleGrantedAuthority对象填充权限列表。

  • 使用用户名及其权限列表创建并最终返回一个新的UserDetail对象。

2) authenticationProvider bean:

<bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsServiceImpl"></property>
</bean>

以之前的userDetailsS​​erviceImpl bean 作为参考。

用户从UserDetailsS​​ervice 中详细说明的这个 bean。

3) 一个authenticationManagerProviderManager 的一个实例

<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <constructor-arg name="providers">
        <list>
            <ref bean="authenticationProvider"/>
        </list>
    </constructor-arg>
</bean>

这需要一个ProviderManager对象列表(在这种情况下,只有一个由authenticationProvider bean 表示)。 此对象通过 AuthenticationProviders 列表迭代 Authentication 请求,并使用内部声明的角色决定对特定资源的请求是否可接受标签(但我不确定这个断言,如果有误请纠正我)。

好的。 这对我来说很清楚……现在进入第二个项目,我有不同的配置。

第二个项目:

进入spring-security.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-4.0.xsd
                        http://www.springframework.org/schema/security 
                        http://www.springframework.org/schema/security/spring-security-4.0.xsd">

      <http pattern="/resources/**" security="none"/>
       <http auto-config="true" use-expressions="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/login" access="permitAll" />
        .............................................................
        SOME ROLES
        .............................................................
        <logout logout-success-url="/login" logout-url="/logout" />
        <form-login  login-page="/login"  
                     authentication-failure-url="/login?error=true"
                    default-target-url="/"
                    username-parameter="nomeUtente"
                    password-parameter="password"
                    login-processing-url="/j_spring_security_check"/>
        <csrf disabled="true"/>
        <!--  <session-management  invalid-session-url="/sessionTimeout" />-->
    </http> 

    <authentication-manager id="authenticationManager" >
        <authentication-provider>
            <jdbc-user-service data-source-ref="datasource" 
                users-by-username-query="select des_usr_par, des_psw_par,true from TID001_ANAGPARTECIPA where des_usr_par =?"
                 authorities-by-username-query="select des_usr_par, prg_par from TID001_ANAGPARTECIPA where des_usr_par = ? "/>

自动检索对存储用户的表执行查询的权限。

那么将 Spring Security 插入到项目中的更好方法是什么? 第一个还是更紧凑的第二个?

        </authentication-provider>
    </authentication-manager>

</beans:beans>

正如您在这种情况下所看到的,它没有声明返回一个UserDetails对象的服务(使用 DAO),该对象由ProviderManager使用的authenticationProvider bean 使用。

在这种情况下,我只有这个声明:

<authentication-manager id="authenticationManager" >
    <authentication-provider>
        <jdbc-user-service data-source-ref="datasource" 
            users-by-username-query="select des_usr_par, des_psw_par,true from TID001_ANAGPARTECIPA where des_usr_par =?"
             authorities-by-username-query="select des_usr_par, prg_par from TID001_ANAGPARTECIPA where des_usr_par = ? "/>

    </authentication-provider>
</authentication-manager>

我认为它会自动声明一个具有 id="authenticationManager"(但它的具体类型是什么?)的身份验证管理器 bean,它使用另一个身份验证提供程序bean(但它的具体类型是什么?)

这完全取决于您的需要。 Spring 提供了自己的用于身份验证和授权的类。 但是如果你想自己做,你可以使用第二个选项。 即用户详细信息服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM