Commit e00248b9 authored by Liang Ding's avatar Liang Ding

重构请求路由 #61

parent 3dc658dc
......@@ -454,12 +454,12 @@ public final class Server extends BaseServer {
private static void routeConsoleProcessors() {
final BeanManager beanManager = BeanManager.getInstance();
final ConsoleAuthAdvice consoleAuthAdvice = beanManager.getReference(ConsoleAuthAdvice.class);
final ConsoleAdminAuthAdvice consoleAdminAuthAdvice = beanManager.getReference(ConsoleAdminAuthAdvice.class);
final ConsoleAuthMidware consoleAuthMidware = beanManager.getReference(ConsoleAuthMidware.class);
final ConsoleAdminAuthMidware consoleAdminAuthMidware = beanManager.getReference(ConsoleAdminAuthMidware.class);
final AdminConsole adminConsole = beanManager.getReference(AdminConsole.class);
final Dispatcher.RouterGroup adminConsoleGroup = Dispatcher.group();
adminConsoleGroup.middlewares(consoleAuthAdvice::handle);
adminConsoleGroup.middlewares(consoleAuthMidware::handle);
adminConsoleGroup.get("/admin-index.do", adminConsole::showAdminIndex).
get("/admin-preference.do", adminConsole::showAdminPreferenceFunction).
get("/console/export/sql", adminConsole::exportSQL).
......@@ -482,7 +482,7 @@ public final class Server extends BaseServer {
final ArticleConsole articleConsole = beanManager.getReference(ArticleConsole.class);
final Dispatcher.RouterGroup articleConsoleGroup = Dispatcher.group();
articleConsoleGroup.middlewares(consoleAuthAdvice::handle);
articleConsoleGroup.middlewares(consoleAuthMidware::handle);
articleConsoleGroup.get("/console/article/push2rhy", articleConsole::pushArticleToCommunity).
get("/console/thumbs", articleConsole::getArticleThumbs).
get("/console/article/{id}", articleConsole::getArticle).
......@@ -496,20 +496,20 @@ public final class Server extends BaseServer {
final CommentConsole commentConsole = beanManager.getReference(CommentConsole.class);
final Dispatcher.RouterGroup commentConsoleGroup = Dispatcher.group();
commentConsoleGroup.middlewares(consoleAuthAdvice::handle);
commentConsoleGroup.middlewares(consoleAuthMidware::handle);
commentConsoleGroup.delete("/console/article/comment/{id}", commentConsole::removeArticleComment).
get("/console/comments/{page}/{pageSize}/{windowSize}", commentConsole::getComments).
get("/console/comments/article/{id}", commentConsole::getArticleComments);
final TagConsole tagConsole = beanManager.getReference(TagConsole.class);
final Dispatcher.RouterGroup tagConsoleGroup = Dispatcher.group();
tagConsoleGroup.middlewares(consoleAuthAdvice::handle);
tagConsoleGroup.middlewares(consoleAuthMidware::handle);
tagConsoleGroup.get("/console/tags", tagConsole::getTags).
get("/console/tag/unused", tagConsole::getUnusedTags);
final CategoryConsole categoryConsole = beanManager.getReference(CategoryConsole.class);
final Dispatcher.RouterGroup categoryGroup = Dispatcher.group();
categoryGroup.middlewares(consoleAdminAuthAdvice::handle);
categoryGroup.middlewares(consoleAdminAuthMidware::handle);
categoryGroup.put("/console/category/order/", categoryConsole::changeOrder).
get("/console/category/{id}", categoryConsole::getCategory).
delete("/console/category/{id}", categoryConsole::removeCategory).
......@@ -519,7 +519,7 @@ public final class Server extends BaseServer {
final LinkConsole linkConsole = beanManager.getReference(LinkConsole.class);
final Dispatcher.RouterGroup linkConsoleGroup = Dispatcher.group();
linkConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
linkConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
linkConsoleGroup.delete("/console/link/{id}", linkConsole::removeLink).
put("/console/link/", linkConsole::updateLink).
put("/console/link/order/", linkConsole::changeOrder).
......@@ -529,7 +529,7 @@ public final class Server extends BaseServer {
final PageConsole pageConsole = beanManager.getReference(PageConsole.class);
final Dispatcher.RouterGroup pageConsoleGroup = Dispatcher.group();
pageConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
pageConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
pageConsoleGroup.put("/console/page/", pageConsole::updatePage).
delete("/console/page/{id}", pageConsole::removePage).
post("/console/page/", pageConsole::addPage).
......@@ -539,7 +539,7 @@ public final class Server extends BaseServer {
final PluginConsole pluginConsole = beanManager.getReference(PluginConsole.class);
final Dispatcher.RouterGroup pluginConsoleGroup = Dispatcher.group();
pluginConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
pluginConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
pluginConsoleGroup.put("/console/plugin/status/", pluginConsole::setPluginStatus).
get("/console/plugins/{page}/{pageSize}/{windowSize}", pluginConsole::getPlugins).
post("/console/plugin/toSetting", pluginConsole::toSetting).
......@@ -547,32 +547,32 @@ public final class Server extends BaseServer {
final PreferenceConsole preferenceConsole = beanManager.getReference(PreferenceConsole.class);
final Dispatcher.RouterGroup preferenceConsoleGroup = Dispatcher.group();
preferenceConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
preferenceConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
preferenceConsoleGroup.get("/console/signs/", preferenceConsole::getSigns).
get("/console/preference/", preferenceConsole::getPreference).
put("/console/preference/", preferenceConsole::updatePreference);
final SkinConsole skinConsole = beanManager.getReference(SkinConsole.class);
final Dispatcher.RouterGroup skinConsoleGroup = Dispatcher.group();
skinConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
skinConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
skinConsoleGroup.get("/console/skin", skinConsole::getSkin).
put("/console/skin", skinConsole::updateSkin);
final RepairConsole repairConsole = beanManager.getReference(RepairConsole.class);
final Dispatcher.RouterGroup repairConsoleGroup = Dispatcher.group();
repairConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
repairConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
repairConsoleGroup.get("/fix/restore-signs", repairConsole::restoreSigns).
get("/fix/archivedate-articles", repairConsole::cleanArchiveDateArticles);
final OtherConsole otherConsole = beanManager.getReference(OtherConsole.class);
final Dispatcher.RouterGroup otherConsoleGroup = Dispatcher.group();
otherConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
otherConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
otherConsoleGroup.delete("/console/archive/unused", otherConsole::removeUnusedArchives).
delete("/console/tag/unused", otherConsole::removeUnusedTags);
final UserConsole userConsole = beanManager.getReference(UserConsole.class);
final Dispatcher.RouterGroup userConsoleGroup = Dispatcher.group();
userConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
userConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
userConsoleGroup.put("/console/user/", userConsole::updateUser).
delete("/console/user/{id}", userConsole::removeUser).
get("/console/users/{page}/{pageSize}/{windowSize}", userConsole::getUsers).
......@@ -581,7 +581,7 @@ public final class Server extends BaseServer {
final StaticSiteConsole staticSiteConsole = beanManager.getReference(StaticSiteConsole.class);
final Dispatcher.RouterGroup staticSiteConsoleGroup = Dispatcher.group();
staticSiteConsoleGroup.middlewares(consoleAdminAuthAdvice::handle);
staticSiteConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
staticSiteConsoleGroup.put("/console/staticsite", staticSiteConsole::genSite);
}
......
......@@ -191,7 +191,7 @@ public class CommentProcessor {
* @param context the specified HTTP request context
*/
private void fillCommenter(final JSONObject requestJSONObject, final RequestContext context) {
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (null == currentUser) {
return;
}
......
......@@ -163,6 +163,12 @@ public class IndexProcessor {
* @param context the specified context
*/
public void showStart(final RequestContext context) {
if (initService.isInited() && null != Solos.getCurrentUser(context)) {
context.sendRedirect(Latkes.getServePath());
return;
}
String referer = context.param("referer");
if (StringUtils.isBlank(referer)) {
referer = context.header("referer");
......
......@@ -118,7 +118,7 @@ public class AdminConsole {
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs);
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
final String userName = currentUser.optString(User.USER_NAME);
dataModel.put(User.USER_NAME, userName);
final String roleName = currentUser.optString(User.USER_ROLE);
......
......@@ -180,7 +180,7 @@ public class ArticleConsole {
context.setRenderer(renderer);
try {
final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
......@@ -298,7 +298,7 @@ public class ArticleConsole {
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
try {
if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
......@@ -344,7 +344,7 @@ public class ArticleConsole {
try {
final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
......@@ -492,7 +492,7 @@ public class ArticleConsole {
final String articleId = article.getString(Keys.OBJECT_ID);
renderer.setJSONObject(ret);
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
ret.put(Keys.STATUS_CODE, false);
......@@ -554,7 +554,7 @@ public class ArticleConsole {
final JSONObject ret = new JSONObject();
try {
final JSONObject requestJSONObject = context.requestJSON();
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
requestJSONObject.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_AUTHOR_ID, currentUser.getString(Keys.OBJECT_ID));
// 打印请求日志,如果发生特殊情况丢失数据,至少还可以根据日志寻回内容
......
......@@ -90,7 +90,7 @@ public class CommentConsole {
try {
final String commentId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (!commentQueryService.canAccessComment(commentId, currentUser)) {
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
......
......@@ -22,19 +22,21 @@ import org.b3log.latke.ioc.Singleton;
import org.b3log.solo.util.Solos;
/**
* The common auth check before advice for admin console.
* The common auth check middleware for admin console.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.0.0.0, Feb 9, 2020
* @since 2.9.5
*/
@Singleton
public class ConsoleAdminAuthAdvice {
public class ConsoleAdminAuthMidware {
public void handle(final RequestContext context) {
if (!Solos.isAdminLoggedIn(context)) {
context.sendError(401);
context.abort();
return;
}
context.handle();
......
......@@ -25,20 +25,22 @@ import org.b3log.solo.util.Solos;
import org.json.JSONObject;
/**
* The common auth check before advice for admin console.
* The common auth check middleware for admin console.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.0.0.0, Feb 9, 2020
* @since 2.9.5
*/
@Singleton
public class ConsoleAuthAdvice {
public class ConsoleAuthMidware {
public void handle(final RequestContext context) {
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (null == currentUser) {
context.sendError(401);
context.abort();
return;
}
final String userRole = currentUser.optString(User.USER_ROLE);
......
......@@ -565,7 +565,7 @@ public class DataModelService {
dataModel.put(Keys.Server.SERVER, Latkes.getServer());
dataModel.put(Common.IS_INDEX, "/".equals(context.requestURI()));
dataModel.put(User.USER_NAME, "");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
if (null != currentUser) {
final String userAvatar = currentUser.optString(UserExt.USER_AVATAR);
dataModel.put(Common.GRAVATAR, userAvatar);
......@@ -625,7 +625,7 @@ public class DataModelService {
}
dataModel.put(Option.ID_C_META_DESCRIPTION, metaDescription);
dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
dataModel.put(Common.IS_LOGGED_IN, null != Solos.getCurrentUser(context.getRequest(), context.getResponse()));
dataModel.put(Common.IS_LOGGED_IN, null != Solos.getCurrentUser(context));
dataModel.put(Common.FAVICON_API, Solos.FAVICON_API);
final String noticeBoard = preference.getString(Option.ID_C_NOTICE_BOARD);
dataModel.put(Option.ID_C_NOTICE_BOARD, noticeBoard);
......@@ -956,7 +956,7 @@ public class DataModelService {
final Template topBarTemplate = Skins.getTemplate("common-template/top-bar.ftl");
final StringWriter stringWriter = new StringWriter();
final Map<String, Object> topBarModel = new HashMap<>();
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = Solos.getCurrentUser(context);
Keys.fillServer(topBarModel);
topBarModel.put(Common.IS_LOGGED_IN, false);
......
......@@ -188,7 +188,7 @@ public final class Solos {
*/
public static JSONObject getUploadToken(final RequestContext context) {
try {
final JSONObject currentUser = getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject currentUser = getCurrentUser(context);
if (null == currentUser) {
return null;
}
......@@ -275,16 +275,17 @@ public final class Solos {
/**
* Gets the current logged-in user.
*
* @param request the specified request
* @param response the specified response
* @param context the specified context
* @return the current logged-in user, returns {@code null} if not found
*/
public static JSONObject getCurrentUser(final Request request, final Response response) {
public static JSONObject getCurrentUser(final RequestContext context) {
final Request request = context.getRequest();
final Set<Cookie> cookies = request.getCookies();
if (cookies.isEmpty()) {
return null;
}
final Response response = context.getResponse();
final BeanManager beanManager = BeanManager.getInstance();
final UserRepository userRepository = beanManager.getReference(UserRepository.class);
try {
......@@ -376,7 +377,7 @@ public final class Solos {
* @return {@code true} if the current request is made by logged in user, returns {@code false} otherwise
*/
public static boolean isLoggedIn(final RequestContext context) {
return null != Solos.getCurrentUser(context.getRequest(), context.getResponse());
return null != Solos.getCurrentUser(context);
}
/**
......@@ -387,7 +388,7 @@ public final class Solos {
* administrator, returns {@code false} otherwise
*/
public static boolean isAdminLoggedIn(final RequestContext context) {
final JSONObject user = getCurrentUser(context.getRequest(), context.getResponse());
final JSONObject user = getCurrentUser(context);
if (null == user) {
return false;
}
......@@ -435,8 +436,7 @@ public final class Solos {
}
}
final Response response = context.getResponse();
final JSONObject currentUser = getCurrentUser(request, response);
final JSONObject currentUser = getCurrentUser(context);
return !(null != currentUser && !Role.VISITOR_ROLE.equals(currentUser.optString(User.USER_ROLE)));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment