From cab1ee9b6ed6855cf394a0d67b0ec0ac4fccbfac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Wed, 14 Dec 2016 22:55:21 +0100 Subject: [PATCH] Spring Boot : make login form work again. --- libresonic-main/pom.xml | 4 ++ .../player/controller/ControllerUtils.java | 35 +++++++++++++ .../player/controller/IndexController.java | 50 +++++++++++++++++++ .../player/controller/LoginController.java | 2 +- .../player/controller/MultiController.java | 35 +------------ .../resources/applicationContext-security.xml | 6 ++- .../src/main/webapp/WEB-INF/jsp/include.jsp | 1 + .../src/main/webapp/WEB-INF/jsp/login.jsp | 4 +- pom.xml | 1 - 9 files changed, 99 insertions(+), 39 deletions(-) create mode 100644 libresonic-main/src/main/java/org/libresonic/player/controller/ControllerUtils.java create mode 100644 libresonic-main/src/main/java/org/libresonic/player/controller/IndexController.java diff --git a/libresonic-main/pom.xml b/libresonic-main/pom.xml index 4d7120f8..bd0fe70b 100644 --- a/libresonic-main/pom.xml +++ b/libresonic-main/pom.xml @@ -69,6 +69,10 @@ org.springframework.security spring-security-ldap + + org.springframework.security + spring-security-taglibs + diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/ControllerUtils.java b/libresonic-main/src/main/java/org/libresonic/player/controller/ControllerUtils.java new file mode 100644 index 00000000..7f08f919 --- /dev/null +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/ControllerUtils.java @@ -0,0 +1,35 @@ +package org.libresonic.player.controller; + +import org.apache.commons.lang.ObjectUtils; +import org.libresonic.player.service.SettingsService; + +import javax.servlet.http.HttpServletRequest; + +/** + * This class has been created to refactor code previously present + * in the MultiController. + */ +public class ControllerUtils { + + public static void updatePortAndContextPath(HttpServletRequest request, SettingsService settingsService) { + + int port = Integer.parseInt(System.getProperty("libresonic.port", String.valueOf(request.getLocalPort()))); + int httpsPort = Integer.parseInt(System.getProperty("libresonic.httpsPort", "0")); + + String contextPath = request.getContextPath().replace("/", ""); + + if (settingsService.getPort() != port) { + settingsService.setPort(port); + settingsService.save(); + } + if (settingsService.getHttpsPort() != httpsPort) { + settingsService.setHttpsPort(httpsPort); + settingsService.save(); + } + if (!ObjectUtils.equals(settingsService.getUrlRedirectContextPath(), contextPath)) { + settingsService.setUrlRedirectContextPath(contextPath); + settingsService.save(); + } + } + +} diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/IndexController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/IndexController.java new file mode 100644 index 00000000..003b364f --- /dev/null +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/IndexController.java @@ -0,0 +1,50 @@ +package org.libresonic.player.controller; + +import org.libresonic.player.Logger; +import org.libresonic.player.domain.User; +import org.libresonic.player.domain.UserSettings; +import org.libresonic.player.service.SecurityService; +import org.libresonic.player.service.SettingsService; +import org.libresonic.player.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.view.RedirectView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Map; + +@Controller +@RequestMapping("/index") +public class IndexController { + + + private static final Logger LOG = Logger.getLogger(IndexController.class); + + @Autowired + private SecurityService securityService; + @Autowired + private SettingsService settingsService; + + @RequestMapping(method = { RequestMethod.GET}) + public ModelAndView index(HttpServletRequest request) { + ControllerUtils.updatePortAndContextPath(request,settingsService); + UserSettings userSettings = settingsService.getUserSettings(securityService.getCurrentUsername(request)); + + Map map = new HashMap(); + map.put("showRight", userSettings.isShowNowPlayingEnabled() || userSettings.isShowChatEnabled()); + map.put("autoHidePlayQueue", userSettings.isAutoHidePlayQueue()); + map.put("listReloadDelay", userSettings.getListReloadDelay()); + map.put("keyboardShortcutsEnabled", userSettings.isKeyboardShortcutsEnabled()); + map.put("showSideBar", userSettings.isShowSideBar()); + map.put("brand", settingsService.getBrand()); + return new ModelAndView("index", "model", map); + } + + +} diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/LoginController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/LoginController.java index 1c74da4d..46aea3d6 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/LoginController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/LoginController.java @@ -43,7 +43,7 @@ public class LoginController { if (username != null && password != null) { username = StringUtil.urlEncode(username); password = StringUtil.urlEncode(password); - return new ModelAndView(new RedirectView("/j_spring_security_check?"+ + return new ModelAndView(new RedirectView("/login?"+ UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY+"=" + username + "&"+UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY+"=" + password )); diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/MultiController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/MultiController.java index 31dd871d..e472eb94 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/MultiController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/MultiController.java @@ -189,7 +189,7 @@ public class MultiController extends MultiActionController { } public ModelAndView gettingStarted(HttpServletRequest request, HttpServletResponse response) { - updatePortAndContextPath(request); + ControllerUtils.updatePortAndContextPath(request,settingsService); if (request.getParameter("hide") != null) { settingsService.setGettingStartedEnabled(false); @@ -202,19 +202,6 @@ public class MultiController extends MultiActionController { return new ModelAndView("gettingStarted", "model", map); } - public ModelAndView index(HttpServletRequest request, HttpServletResponse response) { - updatePortAndContextPath(request); - UserSettings userSettings = settingsService.getUserSettings(securityService.getCurrentUsername(request)); - - Map map = new HashMap(); - map.put("showRight", userSettings.isShowNowPlayingEnabled() || userSettings.isShowChatEnabled()); - map.put("autoHidePlayQueue", userSettings.isAutoHidePlayQueue()); - map.put("listReloadDelay", userSettings.getListReloadDelay()); - map.put("keyboardShortcutsEnabled", userSettings.isKeyboardShortcutsEnabled()); - map.put("showSideBar", userSettings.isShowSideBar()); - map.put("brand", settingsService.getBrand()); - return new ModelAndView("index", "model", map); - } public ModelAndView exportPlaylist(HttpServletRequest request, HttpServletResponse response) throws Exception { @@ -232,26 +219,6 @@ public class MultiController extends MultiActionController { return null; } - private void updatePortAndContextPath(HttpServletRequest request) { - - int port = Integer.parseInt(System.getProperty("libresonic.port", String.valueOf(request.getLocalPort()))); - int httpsPort = Integer.parseInt(System.getProperty("libresonic.httpsPort", "0")); - - String contextPath = request.getContextPath().replace("/", ""); - - if (settingsService.getPort() != port) { - settingsService.setPort(port); - settingsService.save(); - } - if (settingsService.getHttpsPort() != httpsPort) { - settingsService.setHttpsPort(httpsPort); - settingsService.save(); - } - if (!ObjectUtils.equals(settingsService.getUrlRedirectContextPath(), contextPath)) { - settingsService.setUrlRedirectContextPath(contextPath); - settingsService.save(); - } - } public ModelAndView test(HttpServletRequest request, HttpServletResponse response) { return new ModelAndView("test"); diff --git a/libresonic-main/src/main/resources/applicationContext-security.xml b/libresonic-main/src/main/resources/applicationContext-security.xml index f1903e76..58756ca9 100644 --- a/libresonic-main/src/main/resources/applicationContext-security.xml +++ b/libresonic-main/src/main/resources/applicationContext-security.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security - http://www.springframework.org/schema/security/spring-security.xsd"> + http://www.springframework.org/schema/security/spring-security-4.1.xsd"> @@ -59,7 +59,9 @@ + always-use-default-target="true" + username-parameter="j_username" + password-parameter="j_password"/> diff --git a/libresonic-main/src/main/webapp/WEB-INF/jsp/include.jsp b/libresonic-main/src/main/webapp/WEB-INF/jsp/include.jsp index 5bb93d52..a92579c9 100644 --- a/libresonic-main/src/main/webapp/WEB-INF/jsp/include.jsp +++ b/libresonic-main/src/main/webapp/WEB-INF/jsp/include.jsp @@ -4,5 +4,6 @@ <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> <%@ taglib prefix="sub" uri="http://libresonic.org/taglib/sub" %> <%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %> diff --git a/libresonic-main/src/main/webapp/WEB-INF/jsp/login.jsp b/libresonic-main/src/main/webapp/WEB-INF/jsp/login.jsp index 4218a07a..d3081ef7 100644 --- a/libresonic-main/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/libresonic-main/src/main/webapp/WEB-INF/jsp/login.jsp @@ -12,7 +12,9 @@ -
" method="POST"> + + +
diff --git a/pom.xml b/pom.xml index 391fce3e..9fba80a9 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,6 @@ true iso-8859-1 3.1.8 - 3.2.9.RELEASE