簡體   English   中英

使用Spring Security授權

[英]Authorization using Spring Security

我的任務是使用spring-security概念登錄到應用程序。 我能夠使用Spring安全性進行登錄,但是它僅適用於spring-security.xml中的“ ROLE_ADMIN”(因為我已包含( <user name="Sriram" password="12345" authorities="ROLE_USER" /> )。 ,

因此,只有Sriram可以登錄該應用程序。 現在我的問題是,我應該使用下面的憑據( <user name="Yogesh" password="12345" authorities="DIRECT_CUSTOMER" /> )登錄到應用程序。 這意味着驗證應針對特定用戶進行。

  • 情況1:如果Sriram登錄->他只能看到其授權的下一頁。
  • 情況2:如果Yogesh登錄->他只能看到其授權的下一頁。

這意味着,它必須在spring-security.xml中驗證登錄名。

這是我的spring-security.xml:

<http auto-config="true" access-denied-page="/accessDenied.jsp">
        <intercept-url pattern="/"  access="ROLE_USER"/>
        <form-login login-page="/login" default-target-url="/login"
            authentication-failure-url="/loginfailed" />
        <logout logout-success-url="/logout" />  
    </http>

    <authentication-manager>  
   <authentication-provider>  
     <user-service>  
  <user name="Sriram" password="12345" authorities="ROLE_USER" />  
  <!--  <user name="Vignesh" password="12345" authorities="ROLE_ADMIN" /> -->
<!--   <user name="Yogesh" password="12345" authorities="PARTNER_CUSTOMER" />  -->
     </user-service>  
   </authentication-provider>  
 </authentication-manager>

是的,僅對spring-security.xml中提供的用戶進行驗證

<user-service>  
  <user name="Sriram" password="12345" authorities="ROLE_USER" />  
  <user name="Vignesh" password="12345" authorities="ROLE_ADMIN" />
  <user name="Yogesh" password="12345" authorities="PARTNER_CUSTOMER" />
 </user-service>

您不必注釋掉其他用戶。 只需在登錄時提供憑據,安全框架就會將輸入的詳細信息與<user-service>標記下的條目進行匹配。

如果有大量用戶,則可以在<user-service>標記下創建用戶數據庫並從中獲取詳細信息。 檢查一下

您可以設置基於角色的訪問控制。 每個用戶都有各自的角色,然后您可以根據角色映射ant樣式的url。 請參閱以下示例代碼

<http auto-config="true">  
  <access-denied-handler error-page="/403page" />  
  <intercept-url pattern="/user**" access="ROLE_USER" />  
  <intercept-url pattern="/admin**" access="ROLE_ADMIN" />  
  <form-login login-page='/login' username-parameter="username"  
   password-parameter="password" default-target-url="/user"  
   authentication-failure-url="/login?authfailed" />  
  <logout logout-success-url="/login?logout" />  
 </http>

具有特定角色的用戶將只能訪問按角色映射的URL。 因此Vignesh將無法訪問url /admin**他只能訪問/user**

在這里我找到了答案。 工作spring-security.xml在這里

<intercept-url pattern="/" access="ROLE_USER" />

 <user-service>  
  <user name="Sriram" password="12345" authorities="ROLE_USER" /> 
   <user name="Yogesh" password="12345" authorities="ROLE_USER" /> 
  <user name="Vignesh" password="12345" authorities="ROLE_USER" /> 
   <user name="Shruti" password="12345" authorities="ROLE_USER" /> 
   <user name="Umesh" password="12345" authorities="ROLE_USER" /> 
  </user-service> 

我在HomeController.java中對其進行了控制,以驗證用戶是否可以將其發布到其授權頁面。

@RequestMapping(value="/", method = RequestMethod.GET)
        public String printWelcome(ModelMap model) {

            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String name = user.getUsername();
            System.out.println(name);
            String password=user.getPassword();
            System.out.println(password);
            if(name.equals("Sriram") && (password.equals("12345"))){
                return "post-login_SoXAdmin";
            }else if(name.equals("Yogesh") && (password.equals("12345"))){
                return "post-login_directCustomerAdmin";
            }else if(name.equals("Vignesh") && (password.equals("12345"))){
                return "post-login_PartnerAdmin";
            }else if(name.equals("Shruti") && (password.equals("12345"))){
                return "post-login_directCustomerUser";
            }else if(name.equals("Umesh") && (password.equals("12345"))){
                return "post-login_partnerUser";
            }

            else{
                System.out.println("No set of credential found, Please register");
            }
            System.out.println("1" +name);
            System.out.println("SoX_Admin_Page");
            model.addAttribute("username", name);
            model.addAttribute("message", "Spring Security login + database example");
            return "post-login";
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM