Java tutorial
/* * Copyright (c) 2012-2016, b3log.org & hacpai.com & fangstar.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.b3log.symphony.processor.advice; import java.util.Map; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.b3log.latke.Keys; import org.b3log.latke.Latkes; import org.b3log.latke.logging.Logger; import org.b3log.latke.service.LangPropsService; import org.b3log.latke.servlet.HTTPRequestContext; import org.b3log.latke.servlet.advice.BeforeRequestProcessAdvice; import org.b3log.latke.servlet.advice.RequestProcessAdviceException; import org.b3log.symphony.model.Common; import org.b3log.symphony.util.Sessions; import org.json.JSONObject; /** * CSRF check. * * @author <a href="http://88250.b3log.org">Liang Ding</a> * @version 1.0.0.2, Apr 19, 2016 * @since 1.3.0 */ @Named @Singleton public class CSRFCheck extends BeforeRequestProcessAdvice { /** * Logger. */ private static final Logger LOGGER = Logger.getLogger(CSRFCheck.class.getName()); /** * Language service. */ @Inject private LangPropsService langPropsService; @Override public void doAdvice(final HTTPRequestContext context, final Map<String, Object> args) throws RequestProcessAdviceException { final HttpServletRequest request = context.getRequest(); final JSONObject exception = new JSONObject(); exception.put(Keys.MSG, langPropsService.get("csrfCheckFailedLabel")); exception.put(Keys.STATUS_CODE, false); // 1. Check Referer final String referer = request.getHeader("Referer"); if (!StringUtils.startsWith(referer, Latkes.getServePath())) { throw new RequestProcessAdviceException(exception); } // 2. Check Token final String clientToken = request.getHeader(Common.CSRF_TOKEN); final String serverToken = Sessions.getCSRFToken(request); if (!StringUtils.equals(clientToken, serverToken)) { throw new RequestProcessAdviceException(exception); } } }