You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2016/04/08 17:42:01 UTC

svn commit: r1738260 - in /myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src: main/java/org/apache/myfaces/tobago/example/demo/ main/webapp/ main/webapp/WEB-INF/ test/tomee/ test/tomee/conf/

Author: lofwyr
Date: Fri Apr  8 15:42:00 2016
New Revision: 1738260

URL: http://svn.apache.org/viewvc?rev=1738260&view=rev
Log:
security demo

Added:
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Login.java
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/login.xhtml
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/tomcat-users.xml
Modified:
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/web.xml

Added: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Login.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Login.java?rev=1738260&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Login.java (added)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Login.java Fri Apr  8 15:42:00 2016
@@ -0,0 +1,61 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.enterprise.context.RequestScoped;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.inject.Named;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@Named
+@RequestScoped
+public class Login {
+
+  private static final Logger LOG = LoggerFactory.getLogger(Login.class);
+
+  private String username;
+  private String password;
+
+  public void login() throws ServletException, IOException {
+    final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
+    final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
+    final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
+
+    LOG.info("Try to login user: '{}'", username);
+    request.login(username, password);
+    LOG.info("Successful login user: '{}'", username);
+
+    response.sendRedirect(response.encodeRedirectURL("/content/30-concept/80-security/content-security-policy.xhtml"));
+  }
+
+  public void logout() throws ServletException, IOException {
+    final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
+    final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
+    final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
+
+    request.logout();
+
+    response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/"));
+  }
+
+  public String getUsername() {
+    return username;
+  }
+
+  public void setUsername(String username) {
+    this.username = username;
+  }
+
+  public String getPassword() {
+    return password;
+  }
+
+  public void setPassword(String password) {
+    this.password = password;
+  }
+}

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/web.xml?rev=1738260&r1=1738259&r2=1738260&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/web.xml Fri Apr  8 15:42:00 2016
@@ -170,4 +170,29 @@
     </cookie-config>
   </session-config>
 
+  <security-constraint>
+    <display-name>Security Constraint</display-name>
+    <web-resource-collection>
+      <web-resource-name>Application Area</web-resource-name>
+      <url-pattern>/faces/content/*</url-pattern>
+      <url-pattern>/content/*</url-pattern>
+    </web-resource-collection>
+    <auth-constraint>
+      <role-name>demo-user</role-name>
+    </auth-constraint>
+  </security-constraint>
+
+  <login-config>
+    <auth-method>FORM</auth-method>
+    <realm-name>demo-realm</realm-name>
+    <form-login-config>
+      <form-login-page>/faces/login.xhtml</form-login-page>
+      <form-error-page>/faces/login.xhtml</form-error-page>
+    </form-login-config>
+  </login-config>
+
+  <security-role>
+    <role-name>demo-user</role-name>
+  </security-role>
+
 </web-app>

Added: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/login.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/login.xhtml?rev=1738260&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/login.xhtml (added)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/login.xhtml Fri Apr  8 15:42:00 2016
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+
+<ui:composition template="/main.xhtml"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="http://java.sun.com/jsf/facelets">
+
+  <ui:param name="title" value="Login"/>
+
+  <tc:section label="Login">
+
+    <tc:out value="Use one of the following users to login."/>
+    (
+    <tc:link label="guest/guest" omit="true">
+      <tc:dataAttribute name="login" value='{"username": "guest", "password": "guest"}'/>
+    </tc:link>
+    or
+    <tc:link label="admin/admin" omit="true">
+      <tc:dataAttribute name="login" value='{"username": "admin", "password": "admin"}'/>
+    </tc:link>
+    )
+
+    <tc:in id="username" value="#{login.username}" label="User"/>
+    <tc:in id="password" value="#{login.password}" password="true" label="Password"/>
+
+    <tc:button action="#{login.login}" label="Login" defaultCommand="true"/>
+
+  </tc:section>
+
+</ui:composition>

Added: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/tomcat-users.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/tomcat-users.xml?rev=1738260&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/tomcat-users.xml (added)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/test/tomee/conf/tomcat-users.xml Fri Apr  8 15:42:00 2016
@@ -0,0 +1,8 @@
+<?xml version='1.0' encoding='utf-8'?>
+
+<tomcat-users>
+  <role rolename="addressbook-user"/>
+  <user username="guest" password="guest" roles="demo-user"/>
+  <user username="admin" password="admin" roles="demo-user"/>
+</tomcat-users>
+