An authorization filter through tokens was written in the project, but without using spring-security.
This is the normal RestAuthenticationFilter
class, which is inherited from Filter
and is recorded in the property web.xml
.
Now you need to finish the security guard, write some config file that would work like spring-context.xml
. Checked the user to the right of access to a particular url.
In addition, in spring-security, you can check which user is currently logged in with the Principal
, but in this case this option will not work.
Questions:
How in such a RESTful Api with a custom security officer, add your config file, which will set the users access to the materials?
How can I check such a system, the logged in user and give all the information about him (username, password, email ....)?
Update
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter) throws IOException, ServletException { final HttpServletRequest httprequest = (HttpServletRequest) request; final HttpServletResponse httpresponse = (HttpServletResponse) response; final HttpSession session = httprequest.getSession(); boolean isNewSession = session.isNew(); AccountInfo accountInfo = null; if (isNewSession) { log.debug("New Session get user Info"); try { accountInfo = this.isAuthenticated(httprequest, httpresponse); if (accountInfo != null) { session.setAttribute("accountInfo", accountInfo); request.setAttribute("accountInfo", accountInfo); request.setAttribute(SM_USER, accountInfo.getUserName()); System.out.println("AND WHERE IS THIS FUCKIN USER?!!!!"); } else { httpresponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } catch (Exception e) { httprequest.setAttribute("message", "Please contact your administrator to get an access to this application " + e.getMessage()); return; } } else { accountInfo = (AccountInfo) session.getAttribute("accountInfo"); if (accountInfo == null || accountInfo.getAccountId() == null || accountInfo.getAccountId() <= 0) { // ReValidate try { accountInfo = this.isAuthenticated(httprequest, httpresponse); if (accountInfo != null) { session.setAttribute("accountInfo", accountInfo); request.setAttribute("accountInfo", accountInfo); request.setAttribute(SM_USER, accountInfo.getUserName()); } else { httpresponse .setStatus(HttpServletResponse.SC_UNAUTHORIZED); httprequest.setAttribute("message", "Please contact your administrator to get an access to this application " + "Invalid User Info"); } } catch (Exception e) { httprequest.setAttribute("message", "Please contact your administrator to get an access to this application " + e.getMessage()); return; } } else { request.setAttribute(SM_USER, accountInfo.getUserName()); request.setAttribute("accountInfo", session.getAttribute("accountInfo")); accountInfo = (AccountInfo) session.getAttribute("accountInfo"); log.debug("user in old Session"); } } // Get user from session // Set User Context info try { if (session.getAttribute("accountInfo") != null) { AccountService acctService = springContext .getBean(AccountService.class); AccountInfo info = (AccountInfo) session .getAttribute("accountInfo"); if (info.getAccountId() != null) { Account val = acctService.get(accountInfo.getAccountId()); ProfileContext.setAccountContext(val); } // Set this user Name to log4j MDC MDC.put("userName", AbstractServiceModel .formatUserDetails(ProfileContext.getProfileContext())); } } catch (Exception e) { log.debug("Invalid user information" + e); return; } if (httprequest.getRequestURI() != null && httprequest.getRequestURI().contains("logoutApp")) { session.invalidate(); } filter.doFilter(request, response); // End setting // remove context this.destroy(); }