2013-10-15 83 views
1

好日子的人。如何自定義身份驗證提供者的工作

請幫忙。無法理解我的自定義身份驗證提供程序應如何觸發。

我:

彈簧的context.xml

<security:http pattern="/login" security="none" /> 


      <security:http auto-config="true" use-expressions="true"> 


       <security:form-login login-page="/login"/> 

       <security:intercept-url pattern="/" access="hasRole('ROLE_USER')"/> 

       <security:form-login authentication-failure-url="www.google.com"/> 

      </security:http> 



      <security:authentication-manager> 

       <security:authentication-provider user-service-ref="userSecurityService"/> 

      </security:authentication-manager> 


      <bean id="webContentDAOImpl" class="demidov.pkg.persistence.WebContentDAOImpl"> 
       <property name="sessionFactory"><ref bean="sessionFactory"/></property> 
      </bean> 


      <bean id="userSecurityService" class="demidov.pkg.persistence.UserSecurityService"> 
       <property name="webContentDAOIF" > 
        <ref bean="webContentDAOImpl"/> 
       </property> 
      </bean> 

登錄控制器:

@Controller 
public class LoginController { 

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

     return "login"; 
    } 


    @RequestMapping(value="/security/j_spring_security_check", method=RequestMethod.POST) 
    public String access() { 

     return "redirect:/"; 

    } 


} 

登錄JSP頁面:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 

    <form action="security/j_spring_security_check" method="post"> 

     UserName: <input type="text"/> <br> 
     Password: <input type="password"/> <br> 

     <br> 

     <input type="submit"/> 


    </form> 

</body> 
</html> 

自定義主要的解析器:

public class UserSecurityService implements UserDetailsService{ 


    WebContentDAOIF webContentDAOIF; 

     public WebContentDAOIF getWebContentDAOIF() { 
      return webContentDAOIF; 
     } 


     public void setWebContentDAOIF(WebContentDAOIF webContentDAOIF) { 
      this.webContentDAOIF = webContentDAOIF; 
     } 


    @Override 
    public UserDetails loadUserByUsername(String userName) 
      throws UsernameNotFoundException { 


     UserDetails userDetails = null; 


     TheUser theUser = webContentDAOIF.fetchUserByName(userName); 

     userDetails = new User(theUser.getUserEmale(), theUser.getUserPassword(), true, true, true, true, getAthorities(theUser.getRoleAccess())); 


     return userDetails; 
    } 


    public Collection<GrantedAuthority> getAthorities(String role) { 


     List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2); 


     authList.add(new SimpleGrantedAuthority(" ")); 


     if (role.equals("ROLE_USER")) { 

      authList.add(new SimpleGrantedAuthority("ROLE_USER")); 
      } 

      // Return list of granted authorities 
      return authList; 

    } 

} 

我只是無法理解我的自定義主體解析器應如何處理安全性。它應該如何觸發,並由什麼?當我把錯誤的用戶名和密碼登錄頁面上似乎不符合我的UserSecurityService工作,只是簡單地在彈簧context.xml中再次重定向我登錄頁面上,因爲我hasRole(ROLE_USER)。我相信j_spring_security_check可以做一些事情,但這樣的疑問吧。請幫我理解。

回答

3

麻煩參照下文提到的鏈接,可能是有所幫助: -
    spring security custom authentication

+0

此鏈接如何使它工作(如何構建安全的),但我想知道爲什麼我的UserSecurit yService沒有被調用,爲什麼。通常它是如何工作的? – Vad

0

方法loadUserByUsername由具有PARAM userName其具有值從瀏覽器發佈該用戶名與DB進行比較,密碼被取出從DB並傳遞給其爲具有從瀏覽器張貼所以現在將在內部比較的密碼和密碼UserDetail對象採取相應的行動進行身份驗證

相關問題