【发布时间】:2015-03-16 14:23:21
【问题描述】:
我正在开发一个具有以下要求的 Web 应用程序:
- 允许用户登录
- 在服务器端,用户通过第 3 方 REST Web 服务进行身份验证。
- 如果身份验证成功,REST Web 服务将返回唯一的令牌和密钥。
- 对 REST Web 服务的任何后续请求都必须包含在第 3 点(上文)中收到的令牌。
我正在为 web 应用程序使用 spring-mvc 和 spring security。 所以,我得到了一个可行的解决方案,但是我是 spring 新手,不确定解决方案是否正确。
如果:
- 解决方案是否正确实施?
- 该解决方案是否会以任何方式影响性能?
- 该解决方案是否会造成任何安全漏洞?
谢谢:)
解决方案:
-
我创建了一个 MyUser 对象,它将存储从 REST 服务接收到的附加信息。
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; } } -
然后我创建了一个扩展 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; }
【问题讨论】:
标签: java spring spring-mvc authentication spring-security