3
나는 다음과 같은 요구 사항이있는 웹 응용 프로그램을 개발하고 :스토어 추가 정보 [봄]
- 은 사용자가 서버 측에
- 로그인 할 수 있도록 허용을, 사용자는 제 3 자 통해 인증 REST 웹 서비스.
- 인증이 성공한 경우 REST 웹 서비스는 고유 토큰 및 키를 반환합니다.
- REST 웹 서비스에 대한 모든 후속 요청에는 포인트 3 (위)에서받은 토큰이 포함되어야합니다.
웹 응용 프로그램에 spring-mvc 및 spring security를 사용하고 있습니다. 그래서, 나는 해결책을 얻었지만, 나는 봄이 새 것이고 해결책이 정확한지 확신 할 수 없다.
- 이 솔루션이 제대로 구현됩니다 경우
누군가가 조언을 주시겠습니까?
- 솔루션이 어떤 식 으로든 성능에 영향을 줍니까?
- 솔루션으로 보안 허점이 생깁니 까?
감사합니다 :)
솔루션는 :
- 가 나는 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; } }
- 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; } }
- 나는 인증을 위해 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; } }
마지막으로, 나는 내 컨트롤러
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; }
쿨 - 입력 해 주셔서 감사합니다. 코드도 살펴볼 것입니다. – JustH