Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在项目中插入Spring安全性的最佳方法是什么?我有两种不同的方法_Java_Spring_Spring Mvc_Jakarta Ee_Spring Security - Fatal编程技术网

Java 在项目中插入Spring安全性的最佳方法是什么?我有两种不同的方法

Java 在项目中插入Spring安全性的最佳方法是什么?我有两种不同的方法,java,spring,spring-mvc,jakarta-ee,spring-security,Java,Spring,Spring Mvc,Jakarta Ee,Spring Security,我对SpringSecurity是个新手,我对在两个不同的项目中发现的这两种不同的配置有些怀疑。我想知道是一个比另一个好,还是这些是等效的 项目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/XMLSc

我对SpringSecurity是个新手,我对在两个不同的项目中发现的这两种不同的配置有些怀疑。我想知道是一个比另一个好,还是这些是等效的

项目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安全配置文件中所看到的,我首先声明了受保护的资源和对这些资源的访问权限(什么样的用户可以访问这些资源)

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

1) userDetailsServiceImplcom.demo.security.userDetailsServiceImpl类的实例:

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;
    }

}
公共类UserDetailsServiceImpl实现UserDetailsService{
@凌驾
public UserDetails loadUserByUsername(字符串用户名)引发UsernameNotFoundException{
System.out.println(用户名);
//使用用户名作为键从用户数据库表中获取用户对象:
User User=RegisteryDAO.getUserDAO().getUserByUsername(用户名);
if(user==null){
返回null;
}
列表权限=新建ArrayList();
//使用使用用户角色创建的新SimpleGrantedAuthority对象填充authorites列表:
添加(新的SimpleGrantedAuthority(user.getRole());
//使用用户名及其权限列表创建新的UserDetail对象:
UserDetails UserDetails=new org.springframework.security.core.UserDetails。
用户(User.getUsername(),User.getPassword(),true,true,true,authorities);
返回用户详细信息;
}
}
正如您所看到的,这个bean是Spring提供的UserDetailsService接口的实现。因此,它执行以下操作:

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

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

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

2) authenticationProviderbean:

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

以前面的userDetailsServiceImplbean作为参考

这个bean从一个UserDetailsService获取用户详细信息

3) 一个身份验证管理器,它是ProviderManager的实例

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

这将获取ProviderManager对象的列表(在本例中,只有一个由authenticationProviderbean表示)。此对象通过AuthenticationProviders列表迭代身份验证请求,并使用标记中声明的角色确定对特定资源的请求是否可接受(但我不确定此断言是否正确,如果错误请更正我)

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

第二个项目:

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安全性的更好方法是什么呢?第一个还是更紧凑的第二个

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

</beans:beans>

正如您在本例中所看到的,它并没有声明一个服务(使用DAO),该服务返回一个UserDetails对象,该对象由身份验证提供程序bean使用,该bean由ProviderManager使用

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

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


我认为自动声明一个身份验证管理器bean,其id=“authenticationManager”(但其具体类型是什么?)使用另一个身份验证提供者bean(但其具体类型是什么?

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