GWT Spring Security Integration (PURE GWT, NO JSP)

蓝咒 提交于 2019-11-28 08:31:53

Pure GWT solution:

  1. Do not use http element at all (http tag from config namespace)
  2. Define your AuthenticationRpcService
  3. Add AuthenticationRpcService.authenticate(user,password) method
  4. Inject into AuthenticationServiceImpl AuthenticationProvider bean from security-context.xml
  5. Implement AuthenticationRpcService.authenticate(user,password) as :

    User user = new User(login, password, true, true, true, true, new ArrayList<GrantedAuthority>());
    Authentication auth = new UsernamePasswordAuthenticationToken(user, password,
            new ArrayList<GrantedAuthority>());
    try {
        auth = this.authenticationProvider.authenticate(auth);
    } catch (BadCredentialsException e) {
        throw new ClientSideBadCredentialsException(e.getMessage(), e);
    }
    SecurityContext sc = new SecurityContextImpl();
    sc.setAuthentication(auth);
    
    SecurityContextHolder.setContext(sc);
    
  6. Ensure that spring security filter chain is executed during processing of each your GWT RPC call (to be sure that SecurityContext populated into SecurityContextHolder).

  7. Secure all business services with @RolesAllowed({ "ADMIN_ROLE", "USER_ROLE" }) annotations
  8. Prepare your own ClientSideAcessDeniedException that can be used on client side
  9. In a case of spring AcessDeniedException propogate ClientSideAcessDeniedException to client side
  10. On client side set up UncaughtExceptionHandler via GWT.setUncaughtExceptionHandler
  11. In UncaughtExceptionHandler detect CustomAcessDeniedException and then show error to user.

I've found that all you can protect in gwt using Spring Security are the RPC calls but as far as the gui you can't. Remember they are javascript so there is no url to define in the security configuration. That is why all examples have the jsp login page. You would then have to implement some type of client side security using the userContext for frontend .

You need to figure out why your user is considered anonymous (line 77 in stacktrace). I suppose Spring then sends another redirect (to the login) but the GWT callback doesn't know what to do with HTTP 302?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!