2015-01-18 4 views
3

나는 다음과 같은 요구 사항이있는 웹 응용 프로그램을 개발하고 :스토어 추가 정보 [봄]

  1. 은 사용자가 서버 측에
  2. 로그인 할 수 있도록 허용을, 사용자는 제 3 자 통해 인증 REST 웹 서비스.
  3. 인증이 성공한 경우 REST 웹 서비스는 고유 토큰 및 키를 반환합니다.
  4. REST 웹 서비스에 대한 모든 후속 요청에는 포인트 3 (위)에서받은 토큰이 포함되어야합니다.

웹 응용 프로그램에 spring-mvc 및 spring security를 ​​사용하고 있습니다. 그래서, 나는 해결책을 얻었지만, 나는 봄이 새 것이고 해결책이 정확한지 확신 할 수 없다.

  1. 이 솔루션이 제대로 구현됩니다 경우

    누군가가 조언을 주시겠습니까?

  2. 솔루션이 어떤 식 으로든 성능에 영향을 줍니까?
  3. 솔루션으로 보안 허점이 생깁니 까?

감사합니다 :)


솔루션는 :

  1. 가 나는 REST 서비스에서받은 추가 정보를 저장하는 참고 MyUser 객체를 만들었습니다.
    public class MyUser implements Serializable { 
    
        private static final long serialVersionUID = 5047510412099091708L; 
        private String RestToken; 
        private String RestKey; 
    
        public String getRestToken() { 
         return RestToken; 
        } 
        public void setRestToken(String restToken) { 
         RestToken = restToken; 
        } 
        public String getRestKey() { 
         return RestKey; 
        } 
        public void setRestKey(String restKey) { 
         RestKey = restKey; 
        } 
    } 
    
  2. I는 다음이 UsernamePasswordAuthenticationToken을 연장 MyAuthenticationToken 객체를 생성. 이 개체는 CustomAuthenticationProvider에서 사용됩니다 (아래 3 번 항목 참조).
    public class MyAuthenticationToken extends UsernamePasswordAuthenticationToken { 
    
         private static final long serialVersionUID = 7425814465946838862L; 
         private MyUser myUser; 
    
         public MyAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, MyUser myUser){ 
    
          super(principal, credentials, authorities); 
          this.myUser = myUser; 
         } 
    
         public MyUser getMyUser() { 
          return myUser; 
         } 
    
         public void setMyUser(MyUser myUser) { 
          this.myUser = myUser; 
         } 
        } 
    
  3. 나는 인증을 위해 REST 서비스를 호출 한 후 참고 MyUser 및 myAuthenticationToken 객체에서 추가 정보를 저장하는 사용자 정의 인증 공급자를 만들었습니다.

    public class CustomAuthenticationProvider implements AuthenticationProvider { 
    
         @Override 
         public Authentication authenticate (Authentication authentication) { 
    
          MyUser myUser = new MyUser(); 
          MyAuthenticationToken authenticationToken = null; 
          String name = authentication.getName(); 
          String password = authentication.getCredentials().toString(); 
    
          //Just an example. This section will connect to a web service in order to authenticate the client 
          if (name.equals("justh") && password.equals("123456")) { 
    
           //Set the Token and Key Received from the REST WebService 
           myUser.setRestKey("RestKey"); 
           myUser.setRestToken("RestToken"); 
    
           List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 
           grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
    
           authenticationToken = new MyAuthenticationToken(name, password, grantedAuths, myUser); 
    
           return authenticationToken; 
          } else { 
           return null; 
          } 
         } 
    
  4. 마지막으로, 나는 내 컨트롤러

    public ModelAndView adminPage(Authentication authentication) { 
    
         MyUser user = null; 
         //Get the additional data stored 
         if(authentication instanceof MyAuthenticationToken){ 
          user = ((MyAuthenticationToken)authentication).getMyUser(); 
         } 
    
         ModelAndView model = new ModelAndView(); 
         model.addObject("title", "Spring Security Hello World"); 
         model.addObject("message", "This is protected page - Admin Page!" + authentication.getName() + user.getRestKey() + user.getRestToken()); 
         model.setViewName("admin"); 
         return model; 
        } 
    

답변

1

접근 방식이 바로 하나에 저장된 데이터에 액세스 할 수 있습니다. 요구 사항이 간단한 사용자 이름 암호 인증 흐름을 초과 할 때마다 사용자 정의 AuthenticationManagerAuthentication을 구현해야합니다.

그러나 AuthenticationManager 님의 인터페이스 계약을 반드시 준수하십시오.

나는 으로 smtp/imap 서버를 인증하기 위해 내 webmailer에서 아주 비슷한 작업을했으며 완벽하게 작동합니다.

+0

쿨 - 입력 해 주셔서 감사합니다. 코드도 살펴볼 것입니다. – JustH