Differences

This shows you the differences between two versions of the page.

Link to this comparison view

java:new-app [06/03/2015 13:57]
Anthony Arents [JSON errors]
java:new-app [04/10/2017 11:37] (current)
Anthony Arents [New Java Web Applications]
Line 1: Line 1:
 ====== New Java Web Applications ====== ====== New Java Web Applications ======
 +
 +End of life December 2016.
  
 If you are making a company project : If you are making a company project :
Line 14: Line 16:
   * Tomcat 7   * Tomcat 7
   * Use Java 7 for compiling, I recommend Java 8 for running tomcat (no permgen errors) ;-).   * Use Java 7 for compiling, I recommend Java 8 for running tomcat (no permgen errors) ;-).
 +
 +Tomcat settings \\ 
 +<code>-Xmx2048m -Xms512m -XX:MaxPermSize=768m -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true</code>
  
 ====== Package structure : ====== ====== Package structure : ======
Line 438: Line 443:
 ===== web.xml : ===== ===== web.xml : =====
  
-Now we need the following in our ''WEB-INF/web.xml''+Now we need the following in our ''WEB-INF/web.xml'' \\  
 +Remove Spring Security if you are not using it!
  
 <code xml><?xml version="1.0" encoding="UTF-8"?> <code xml><?xml version="1.0" encoding="UTF-8"?>
Line 1295: Line 1301:
     </bean>     </bean>
          
-    <bean id="loginController" class="de.jcpis.analyse.presentation.LoginController">+    <bean id="loginController" class="be.mentoringsystems.applicationname.presentation.LoginController">
         <property name="methodNameResolver" ref="paramResolver" />         <property name="methodNameResolver" ref="paramResolver" />
         <property name="loginService" ref="loginService" />         <property name="loginService" ref="loginService" />
 +    </bean>
 +
 +    <bean id="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
 +        <property name="viewName" value="app" />
     </bean>     </bean>
  
     <bean id="urlFileViewer" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>     <bean id="urlFileViewer" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>
 +
 +    <bean id="jspViewResolver"
 +   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 + <property name="prefix" value="/WEB-INF/jsp/"/>
 + <property name="suffix" value=".jsp" />
 +    </bean>
  
     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
Line 1306: Line 1322:
         <property name="mappings">         <property name="mappings">
             <props>             <props>
-                <prop key="/login.html">loginController</prop> 
                 <prop key="/login.json">loginController</prop>                 <prop key="/login.json">loginController</prop>
 +                <prop key="/">indexController</prop>
             </props>             </props>
         </property>         </property>
Line 1316: Line 1332:
 </code> </code>
  
 +====== JSP Pages ======
 +===== Login =====
 +
 +Add ''login.jsp'' to ''/WEB-INF/jsp'' \\ 
 +
 +<code xml>
 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 +   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +<html xmlns="http://www.w3.org/1999/xhtml">
 +    <head>
 +        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 +        <title>Login</title>
 +        <link type="text/css" rel="stylesheet" href="static/styles/login.css" />
 +    </head>
 +    <body class="loginPage">
 +<div id="page">
 +<div id="login">
 +    <div id="logincontent">
 +     <h1 id="login-title">Login</h1>
 +        <form action="j_spring_security_check" method="post" id="login-form">
 +        
 +                <div>
 +                    <label for="username">Username :</label>
 +                    <input type='text' id="username" tabindex="1" name='j_username' />
 +                </div>
 +                <div>
 +                    <label for="password">Password :</label>
 +                    <input id="password" tabindex="2" type='password' name='j_password' />
 +                </div>
 +                <div class="submitbtn">
 +     <input type="submit" tabindex="4" value="Login" class="loginButton" />
 +                </div>
 +        </form>
 +</div></div>
 +    </body>
 +</html>
 +</code>
 +
 +You are free to change the implementation of the loginpage, \\ 
 +you'll find it easier to rely on an old fashioned form submit to login a user, the system will then redirect to app.html (app.jsp)
 +===== App =====
 +
 +Add ''app.jsp'' to ''/WEB-INF/jsp'' \\ 
 +
 +this should be the index.html of your extjs application
 +====== Static resources (javascript, css, ...) ======
 +
 +Don'y use html pages, use jsp like above.
 +simply place the resource in ''/static/'' or any subfolder there
 ====== Spring Security ====== ====== Spring Security ======
  
Line 1462: Line 1527:
 ===== Json fixes ===== ===== Json fixes =====
  
-==== Custom Authentication Entry Point ====+We want a json message telling us we need to login when we do an AJAX request. else spring will redirect to the loginpage & our application will break without telling why. (not needed when polling the server)
  
-<code java>package be.mentoringsystems.applicationname.security;+==== Custom Access Denied Handler ==== 
 + 
 +<code java>package be.mentoringsystems.security;
  
 import java.io.IOException; import java.io.IOException;
Line 1470: Line 1537:
 import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
-import org.springframework.security.core.AuthenticationException+import org.springframework.security.access.AccessDeniedException
-import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;+import org.springframework.security.web.access.AccessDeniedHandlerImpl;
 import org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper; import org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper;
  
Line 1478: Line 1545:
  * @author anthonyarents  * @author anthonyarents
  */  */
-public class CustomAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {+public class CustomAccessDeniedHandler extends AccessDeniedHandlerImpl {
  
     @Override     @Override
-    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {+    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
         final String jsonHeader = request.getHeader("X-Requested-With");         final String jsonHeader = request.getHeader("X-Requested-With");
  
Line 1490: Line 1557:
             response.getOutputStream().print("{\"success\" : false, \"data\" : [], \"error\" : \"LOGIN REQUIRED\"}");             response.getOutputStream().print("{\"success\" : false, \"data\" : [], \"error\" : \"LOGIN REQUIRED\"}");
         } else {         } else {
-            super.commence(request, response, authException);+            super.handle(request, response, accessDeniedException);
         }         }
     }     }
 +}
 +</code>
  
-}</code> +==== Custom Authentication Entry Point ====
- +
-==== Custom Access Denied Handler ====+
  
 <code java>package be.mentoringsystems.applicationname.security; <code java>package be.mentoringsystems.applicationname.security;
Line 1504: Line 1571:
 import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
-import org.springframework.security.access.AccessDeniedException+import org.springframework.security.core.AuthenticationException
-import org.springframework.security.web.access.AccessDeniedHandlerImpl;+import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
 import org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper; import org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper;
  
Line 1512: Line 1579:
  * @author anthonyarents  * @author anthonyarents
  */  */
-public class CustomAccessDeniedHandler extends AccessDeniedHandlerImpl {+public class CustomAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
  
     @Override     @Override
-    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {+    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
         final String jsonHeader = request.getHeader("X-Requested-With");         final String jsonHeader = request.getHeader("X-Requested-With");
  
Line 1524: Line 1591:
             response.getOutputStream().print("{\"success\" : false, \"data\" : [], \"error\" : \"LOGIN REQUIRED\"}");             response.getOutputStream().print("{\"success\" : false, \"data\" : [], \"error\" : \"LOGIN REQUIRED\"}");
         } else {         } else {
-            super.handle(request, response, accessDeniedException);+            super.commence(request, response, authException);
         }         }
     }     }
-} + 
-</code>+}</code> 
  
 ===== applicationname-security.xml ===== ===== applicationname-security.xml =====
Line 1548: Line 1616:
         <security:intercept-url pattern="/ws/**" filters="none" />         <security:intercept-url pattern="/ws/**" filters="none" />
         <security:intercept-url pattern="/login.html" filters="none" />         <security:intercept-url pattern="/login.html" filters="none" />
-        <security:intercept-url pattern="/j_spring_security_switch_user" filters="ROLE_ADMIN" />+        <security:intercept-url pattern="/j_spring_security_switch_user" access="ROLE_ADMIN" />
         <!-- useful if we need app.html to also be available for anonymous users -->         <!-- useful if we need app.html to also be available for anonymous users -->
         <!--security:intercept-url pattern="/app.html" access="ROLE_ANONYMOUS, ROLE_USER" /-->         <!--security:intercept-url pattern="/app.html" access="ROLE_ANONYMOUS, ROLE_USER" /-->