Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit 659064f

Browse files
author
Arun Gupta
committed
Starting working on Servlet security sample
Former-commit-id: 0c4ee55b39a4caff41902498799edb0e7968cc7c
1 parent 833dd8f commit 659064f

5 files changed

Lines changed: 280 additions & 0 deletions

File tree

samples/servlet/security/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>servlet</artifactId>
6+
<groupId>org.sample</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.glassfish.sample</groupId>
12+
<artifactId>security</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
16+
<name>security</name>
17+
18+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package org.glassfish.sample.security;
6+
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.annotation.WebServlet;
11+
import javax.servlet.http.HttpServlet;
12+
import javax.servlet.http.HttpServletRequest;
13+
import javax.servlet.http.HttpServletResponse;
14+
15+
/**
16+
*
17+
* @author arungup
18+
*/
19+
@WebServlet(name = "SecureServlet", urlPatterns = {"/SecureServlet"})
20+
public class SecureServlet extends HttpServlet {
21+
22+
/**
23+
* Processes requests for both HTTP
24+
* <code>GET</code> and
25+
* <code>POST</code> methods.
26+
*
27+
* @param request servlet request
28+
* @param response servlet response
29+
* @throws ServletException if a servlet-specific error occurs
30+
* @throws IOException if an I/O error occurs
31+
*/
32+
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String method)
33+
throws ServletException, IOException {
34+
response.setContentType("text/html;charset=UTF-8");
35+
try (PrintWriter out = response.getWriter()) {
36+
out.println("<!DOCTYPE html>");
37+
out.println("<html>");
38+
out.println("<head>");
39+
out.println("<title>Servlet SecureServlet</title>");
40+
out.println("</head>");
41+
out.println("<body>");
42+
out.println("<h1>Servlet SecureServlet at " + request.getContextPath() + " (" + method + ")</h1>");
43+
out.println("</body>");
44+
out.println("</html>");
45+
}
46+
}
47+
48+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
49+
/**
50+
* Handles the HTTP
51+
* <code>GET</code> method.
52+
*
53+
* @param request servlet request
54+
* @param response servlet response
55+
* @throws ServletException if a servlet-specific error occurs
56+
* @throws IOException if an I/O error occurs
57+
*/
58+
@Override
59+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
60+
throws ServletException, IOException {
61+
processRequest(request, response, "GET");
62+
}
63+
64+
/**
65+
* Handles the HTTP
66+
* <code>POST</code> method.
67+
*
68+
* @param request servlet request
69+
* @param response servlet response
70+
* @throws ServletException if a servlet-specific error occurs
71+
* @throws IOException if an I/O error occurs
72+
*/
73+
@Override
74+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
75+
throws ServletException, IOException {
76+
processRequest(request, response, "POST");
77+
}
78+
79+
/**
80+
* Returns a short description of the servlet.
81+
*
82+
* @return a String containing servlet description
83+
*/
84+
@Override
85+
public String getServletInfo() {
86+
return "Short description";
87+
}// </editor-fold>
88+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/*
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5+
*
6+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
7+
*
8+
* The contents of this file are subject to the terms of either the GNU
9+
* General Public License Version 2 only ("GPL") or the Common Development
10+
* and Distribution License("CDDL") (collectively, the "License"). You
11+
* may not use this file except in compliance with the License. You can
12+
* obtain a copy of the License at
13+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
14+
* or packager/legal/LICENSE.txt. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
* When distributing the software, include this License Header Notice in each
18+
* file and include the License file at packager/legal/LICENSE.txt.
19+
*
20+
* GPL Classpath Exception:
21+
* Oracle designates this particular file as subject to the "Classpath"
22+
* exception as provided by Oracle in the GPL Version 2 section of the License
23+
* file that accompanied this code.
24+
*
25+
* Modifications:
26+
* If applicable, add the following below the License Header, with the fields
27+
* enclosed by brackets [] replaced by your own identifying information:
28+
* "Portions Copyright [year] [name of copyright owner]"
29+
*
30+
* Contributor(s):
31+
* If you wish your version of this file to be governed by only the CDDL or
32+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
33+
* elects to include this software in this distribution under the [CDDL or GPL
34+
* Version 2] license." If you don't indicate a single choice of license, a
35+
* recipient has the option to distribute your version of this file under
36+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
37+
* its licensees as provided above. However, if you add GPL Version 2 code
38+
* and therefore, elected the GPL Version 2 license, then the option applies
39+
* only if the new code is made subject to such option by the copyright
40+
* holder.
41+
*/
42+
-->
43+
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
44+
<glassfish-web-app error-url="">
45+
<security-role-mapping>
46+
<role-name>g1</role-name>
47+
<principal-name>g1</principal-name>
48+
<group-name>g1</group-name>
49+
</security-role-mapping>
50+
</glassfish-web-app>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/*
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5+
*
6+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
7+
*
8+
* The contents of this file are subject to the terms of either the GNU
9+
* General Public License Version 2 only ("GPL") or the Common Development
10+
* and Distribution License("CDDL") (collectively, the "License"). You
11+
* may not use this file except in compliance with the License. You can
12+
* obtain a copy of the License at
13+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
14+
* or packager/legal/LICENSE.txt. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
* When distributing the software, include this License Header Notice in each
18+
* file and include the License file at packager/legal/LICENSE.txt.
19+
*
20+
* GPL Classpath Exception:
21+
* Oracle designates this particular file as subject to the "Classpath"
22+
* exception as provided by Oracle in the GPL Version 2 section of the License
23+
* file that accompanied this code.
24+
*
25+
* Modifications:
26+
* If applicable, add the following below the License Header, with the fields
27+
* enclosed by brackets [] replaced by your own identifying information:
28+
* "Portions Copyright [year] [name of copyright owner]"
29+
*
30+
* Contributor(s):
31+
* If you wish your version of this file to be governed by only the CDDL or
32+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
33+
* elects to include this software in this distribution under the [CDDL or GPL
34+
* Version 2] license." If you don't indicate a single choice of license, a
35+
* recipient has the option to distribute your version of this file under
36+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
37+
* its licensees as provided above. However, if you add GPL Version 2 code
38+
* and therefore, elected the GPL Version 2 license, then the option applies
39+
* only if the new code is made subject to such option by the copyright
40+
* holder.
41+
*/
42+
-->
43+
44+
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
45+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
46+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
47+
version="3.0">
48+
<security-constraint>
49+
<web-resource-collection>
50+
<web-resource-name>SecureServlet</web-resource-name>
51+
<url-pattern>/SecureServlet</url-pattern>
52+
<http-method>GET</http-method>
53+
</web-resource-collection>
54+
<auth-constraint>
55+
<role-name>g1</role-name>
56+
</auth-constraint>
57+
</security-constraint>
58+
59+
<login-config>
60+
<auth-method>BASIC</auth-method>
61+
<realm-name>file</realm-name>
62+
</login-config>
63+
64+
<security-role>
65+
<role-name>g1</role-name>
66+
</security-role>
67+
</web-app>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
/*
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4+
*
5+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6+
*
7+
* The contents of this file are subject to the terms of either the GNU
8+
* General Public License Version 2 only ("GPL") or the Common Development
9+
* and Distribution License("CDDL") (collectively, the "License"). You
10+
* may not use this file except in compliance with the License. You can
11+
* obtain a copy of the License at
12+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13+
* or packager/legal/LICENSE.txt. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*
16+
* When distributing the software, include this License Header Notice in each
17+
* file and include the License file at packager/legal/LICENSE.txt.
18+
*
19+
* GPL Classpath Exception:
20+
* Oracle designates this particular file as subject to the "Classpath"
21+
* exception as provided by Oracle in the GPL Version 2 section of the License
22+
* file that accompanied this code.
23+
*
24+
* Modifications:
25+
* If applicable, add the following below the License Header, with the fields
26+
* enclosed by brackets [] replaced by your own identifying information:
27+
* "Portions Copyright [year] [name of copyright owner]"
28+
*
29+
* Contributor(s):
30+
* If you wish your version of this file to be governed by only the CDDL or
31+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
32+
* elects to include this software in this distribution under the [CDDL or GPL
33+
* Version 2] license." If you don't indicate a single choice of license, a
34+
* recipient has the option to distribute your version of this file under
35+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
36+
* its licensees as provided above. However, if you add GPL Version 2 code
37+
* and therefore, elected the GPL Version 2 license, then the option applies
38+
* only if the new code is made subject to such option by the copyright
39+
* holder.
40+
*/
41+
-->
42+
43+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
44+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45+
"http://www.w3.org/TR/html4/loose.dtd">
46+
47+
<html>
48+
<head>
49+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50+
<title>Servlet Security</title>
51+
</head>
52+
<body>
53+
<h1>Servlet Security</h1>
54+
55+
<a href="${pageContext.request.contextPath}/SecureServlet">GET</a><br/>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)