Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring security jdbc配置用于验证具有多个角色的用户_Spring_Spring Mvc_Authentication_Spring Security_Spring Annotations - Fatal编程技术网

Spring security jdbc配置用于验证具有多个角色的用户

Spring security jdbc配置用于验证具有多个角色的用户,spring,spring-mvc,authentication,spring-security,spring-annotations,Spring,Spring Mvc,Authentication,Spring Security,Spring Annotations,我目前正在开发一个基于java的发票应用程序。 我需要的是,授权具有多个角色的用户,例如Peter应该具有角色设置、角色报告、角色引用,而Anne只有一个角色设置等 这是我的spring-security-config.xml <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmln

我目前正在开发一个基于java的发票应用程序。 我需要的是,授权具有多个角色的用户,例如Peter应该具有角色设置、角色报告、角色引用,而Anne只有一个角色设置等

这是我的spring-security-config.xml

<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.2.xsd">  

 <http auto-config="true">  
  <access-denied-handler error-page="/403page" />  
  <intercept-url pattern="/makebill*" access="ROLE_ADMIN,ROLE_MAKEBILL" />
  <intercept-url pattern="/report*" access="ROLE_ADMIN,ROLE_REPORT" />
  <intercept-url pattern="/stock*" access="ROLE_ADMIN,ROLE_STOCK" />
  <intercept-url pattern="/customer*" access="ROLE_ADMIN,ROLE_CUSTOMER" />
  <!-- <intercept-url pattern="/setting*" access="ROLE_ADMIN,ROLE_SETTING" />   -->
  <intercept-url pattern="/mainmenu*" access="ROLE_ADMIN,ROLE_MAKEBILL,ROLE_SETTING,ROLE_CUSTOMER,ROLE_REPORT,ROLE_STOCK" />  
  <form-login 
    login-page='/login' username-parameter="username"  
   password-parameter="password" default-target-url="/mainmenu"  
   authentication-failure-url="/login?authfailed" />  
  <logout logout-success-url="/login?logout" />  
 </http>  

 <authentication-manager> 
     <authentication-provider>  
        <jdbc-user-service 
            data-source-ref="dataSource"  
            users-by-username-query="select username,password, enabled from person where username=?"  
            authorities-by-username-query="select username, role from person, role where username =?  " />  
     </authentication-provider> 
 </authentication-manager> 


</beans:beans>  
<http auto-config="true">  
  <access-denied-handler error-page="/403page" />  
  <intercept-url pattern="/makebill*" access="ROLE_ADMIN,ROLE_MAKEBILL" />
  <intercept-url pattern="/report*" access="ROLE_ADMIN,ROLE_REPORT" />
  <intercept-url pattern="/stock*" access="ROLE_ADMIN,ROLE_STOCK" />
  <intercept-url pattern="/customer*" access="ROLE_ADMIN,ROLE_CUSTOMER" />
  <intercept-url pattern="/setting*" access="ROLE_ADMIN,ROLE_SETTING" />
  <intercept-url pattern="/mainmenu*" access="ROLE_ADMIN,ROLE_MAKEBILL,ROLE_SETTING,ROLE_CUSTOMER,ROLE_REPORT,ROLE_STOCK" />  
  <form-login 
    login-page='/login' username-parameter="username"  
   password-parameter="password" default-target-url="/mainmenu"  
   authentication-failure-url="/login?authfailed" />  
  <logout logout-success-url="/login?logout" />  
 </http>  


 <beans:bean id="daoAuthenticationProvider"
 class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
  <beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
  <beans:property name="providers">
    <beans:list>
      <beans:ref local="daoAuthenticationProvider" />
    </beans:list>
  </beans:property>
</beans:bean>

<authentication-manager>
  <authentication-provider user-service-ref="userDetailsService">

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

身份验证管理器目前只使用一个角色就可以正常工作,我的意思是…当sql查询
Authority by username query=“select username,role from person,role where username=?”
运行时,如果返回的值只是一个角色,例如
role\u REPORT
,则应用程序可以正常工作,但是如果在数据库中有一条记录,比如,
ROLE\u REPORT,ROLE\u SETTING,ROLE\u CUSTOMER
,当查询检索到这个值时,应用程序将返回404错误,似乎我无法授予具有多个角色的用户

谁能指出我做错了什么。
谢谢。

我自己找到了一个解决方案,实现了我自己的用户详细信息,下面是代码

@Service("assembler")
public class Assembler {

  @Transactional(readOnly = true)
  User buildUserFromUserEntity(Person person) {

    String username = person.getUsername();
    String password = person.getPassword();
    boolean enabled = true;

    List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
    String[] authStrings = person.getRole().split(",");
    for(String authString : authStrings) {
        System.out.println("all auth roles: " + authString);
        authorities.add(new SimpleGrantedAuthority(authString));
    }


    User user = new User(username, password, enabled,
      true, true, true, authorities);

    return user;
  }
}






@Service("userDetailsService") 
public class UserDetailsServiceImpl implements UserDetailsService {

  @Autowired private PersonService personService;
  @Autowired private Assembler assembler;

  @Transactional(readOnly = true)
  public UserDetails loadUserByUsername(String username)
      throws UsernameNotFoundException, DataAccessException {

    UserDetails userDetails = null;
    Person person = null;

    List<Person> listPeople = personService.listPeople();
    for (Person p: listPeople) {
        if (p.getUsername().equals(username)) {
            person = p;
        }
    }

    if (person == null)
      throw new UsernameNotFoundException("user not found");

    return assembler.buildUserFromUserEntity(person);
  }
}
@服务(“汇编程序”)
公共类汇编程序{
@事务(只读=真)
用户buildUserFromUserEntity(个人){
字符串username=person.getUsername();
字符串password=person.getPassword();
布尔启用=真;
列表权限=新建ArrayList();
String[]authStrings=person.getRole().split(“,”);
用于(字符串authString:authString){
System.out.println(“所有身份验证角色:+authString”);
添加(新的SimpleGrantedAuthority(authString));
}
用户=新用户(用户名、密码、已启用、,
真的,真的,真的,权威);
返回用户;
}
}
@服务(“userDetailsService”)
公共类UserDetailsServiceImpl实现UserDetailsService{
@自动连线私人个人服务;
@自编专用汇编程序;
@事务(只读=真)
公共用户详细信息loadUserByUsername(字符串用户名)
抛出UsernameNotFoundException、DataAccessException{
UserDetails UserDetails=null;
Person=null;
List listPeople=personService.listPeople();
for(人员p:listPeople){
如果(p.getUsername().equals(username)){
人=p;
}
}
if(person==null)
抛出新的UsernameNotFoundException(“未找到用户”);
返回assembler.buildUserFromUserEntity(person);
}
}
下面是spring-security-config.xml

<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.2.xsd">  

 <http auto-config="true">  
  <access-denied-handler error-page="/403page" />  
  <intercept-url pattern="/makebill*" access="ROLE_ADMIN,ROLE_MAKEBILL" />
  <intercept-url pattern="/report*" access="ROLE_ADMIN,ROLE_REPORT" />
  <intercept-url pattern="/stock*" access="ROLE_ADMIN,ROLE_STOCK" />
  <intercept-url pattern="/customer*" access="ROLE_ADMIN,ROLE_CUSTOMER" />
  <!-- <intercept-url pattern="/setting*" access="ROLE_ADMIN,ROLE_SETTING" />   -->
  <intercept-url pattern="/mainmenu*" access="ROLE_ADMIN,ROLE_MAKEBILL,ROLE_SETTING,ROLE_CUSTOMER,ROLE_REPORT,ROLE_STOCK" />  
  <form-login 
    login-page='/login' username-parameter="username"  
   password-parameter="password" default-target-url="/mainmenu"  
   authentication-failure-url="/login?authfailed" />  
  <logout logout-success-url="/login?logout" />  
 </http>  

 <authentication-manager> 
     <authentication-provider>  
        <jdbc-user-service 
            data-source-ref="dataSource"  
            users-by-username-query="select username,password, enabled from person where username=?"  
            authorities-by-username-query="select username, role from person, role where username =?  " />  
     </authentication-provider> 
 </authentication-manager> 


</beans:beans>  
<http auto-config="true">  
  <access-denied-handler error-page="/403page" />  
  <intercept-url pattern="/makebill*" access="ROLE_ADMIN,ROLE_MAKEBILL" />
  <intercept-url pattern="/report*" access="ROLE_ADMIN,ROLE_REPORT" />
  <intercept-url pattern="/stock*" access="ROLE_ADMIN,ROLE_STOCK" />
  <intercept-url pattern="/customer*" access="ROLE_ADMIN,ROLE_CUSTOMER" />
  <intercept-url pattern="/setting*" access="ROLE_ADMIN,ROLE_SETTING" />
  <intercept-url pattern="/mainmenu*" access="ROLE_ADMIN,ROLE_MAKEBILL,ROLE_SETTING,ROLE_CUSTOMER,ROLE_REPORT,ROLE_STOCK" />  
  <form-login 
    login-page='/login' username-parameter="username"  
   password-parameter="password" default-target-url="/mainmenu"  
   authentication-failure-url="/login?authfailed" />  
  <logout logout-success-url="/login?logout" />  
 </http>  


 <beans:bean id="daoAuthenticationProvider"
 class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
  <beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
  <beans:property name="providers">
    <beans:list>
      <beans:ref local="daoAuthenticationProvider" />
    </beans:list>
  </beans:property>
</beans:bean>

<authentication-manager>
  <authentication-provider user-service-ref="userDetailsService">

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

我现在能够为一个用户完美地分配多个角色。
感谢阅读:)

需要修复的地方很少。首先,按用户名查询权限是不对的。我认为您是SQL查询新手。您正在获取两个表,但没有使用任何键连接它们。请看看这个。这可能无法解决您的所有问题。如果您有其他问题,请告诉我。您可能会发现本教程很有用:mkyong的tut用于为一个用户授予一个角色,我需要的是为一个用户授予多个角色……恐怕没有一行或两行的修复程序。您可能必须拥有自己的UserDetails并设置权限,以便您的用户具有多个角色。我发现这个教程网站对我很有帮助。这与您现在的做法有些不同,因为您似乎没有使用JPA或hibernate。