Добрый день.
В модуле портал приложения есть анонимные REST сервисы (в rest-dispatcher для них выставлено security=“none”) доступные через /rest/anonymous/**.
Некоторые из них обращаются к сервисам среднего слоя, что вызывает ошибку “no security context”. Чтобы это исправить нужно задать SecurityContext для данных сервисов.
Поскольку указанных сервисов достаточно большое количество (и будет становиться больше) нужно было задание контекста автоматизировать.
Для этого был сделан следующий фильтр:
public class CubaGisRestAnonymousFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
SecurityContext cachedSecurityContext = AppContext.getSecurityContext();
try {
AppContext.setSecurityContext(new SecurityContext(CubaPlatformUtils.getAnonymousUserSession()));
chain.doFilter(request, response);
} catch (LoginException e) {
throw new ServletException(e);
} finally {
AppContext.setSecurityContext(cachedSecurityContext);
}
}
CubaPlatformUtils
public class CubaPlatformUtils {
public static PortalConfig getPortalConfig() {
Configuration configuration = AppBeans.get(Configuration.class);
return configuration.getConfig(PortalConfig.class);
}
public static UserSession getAnonymousUserSession() throws LoginException {
return AppBeans.get(TrustedClientService.class).getAnonymousSession(getPortalConfig().getTrustedClientPassword());
}
web.xml
<filter>
<filter-name>restAnonymousFilter</filter-name>
<filter-class>com.company.portal.restapi.filters.CubaGisRestAnonymousFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restAnonymousFilter</filter-name>
<url-pattern>/rest/anonymous/*</url-pattern>
</filter-mapping>
Скажите пожалуйста насколько использованный метод правильный с точки зрения платформы и если он таковым не является укажите в каком направлении двигаться?
Спасибо.