Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring Security 3.x Cookbook

You're reading from   Spring Security 3.x Cookbook Secure your Java applications against online threats by learning the powerful mechanisms of Spring Security. Presented as a cookbook full of recipes, this book covers a wide range of vulnerabilities and scenarios.

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781782167525
Length 300 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Anjana Mankale Anjana Mankale
Author Profile Icon Anjana Mankale
Anjana Mankale
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Basic Security FREE CHAPTER 2. Spring Security with Struts 2 3. Spring Security with JSF 4. Spring Security with Grails 5. Spring Security with GWT 6. Spring Security with Vaadin 7. Spring Security with Wicket 8. Spring Security with ORM and NoSQL DB 9. Spring Security with Spring Social 10. Spring Security with Spring Web Services 11. More on Spring Security Index

Basic authentication for JAX-WS and JAX-RS

The authentication configuration remains the same for JAX-WS and JAX-RS.

We need to give the JAX-WS or JAX-RS URL in <web-resource collection>.

Auth_type can be basic. The container would come with a form for the user to enter the username and password.

Authentication handled by container

We will first create a web service and then make the container handle the security on it.

Let's create an interface which will expose the service method and then declare an implementation class.

Let's use Tomcat 6.0 to demonstrate this.

Getting ready

  • In Eclipse-Indigo, create a dynamic web project
  • Server: Tomcat 6
  • JARs to be added to Tomcat lib folder: https://jax-ws.java.net/2.2.7/
  • Download the project and copy the lib folder

How to do it...

  1. Create an interface and an implementation class. Add the @WebService annotations to it. Create a package named com.packt.ws. Create an interface named EmployeeProfile and an implementation Class:

    Interface:

    package com.packt.ws;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    @WebService
    @SOAPBinding(style = Style.RPC)
    public interface EmployeeProfile {
      @WebMethod
      String getSalary();
    }

    Implementation:

    package com.packt.ws;
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    @WebService(endpointInterface = "com.packt.ws.EmployeeProfile")
    public class EmployeeProfileImpl implements EmployeeProfile {
             @Override
    public String getSalary() {
        return "no salary for the month";
    }
    }
  2. Also add the sun-jaxws.xml file under WEB-INF
    <?xml version="1.0" encoding="UTF-8"?>
    <endpoints
      xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
      version="2.0">
      <endpoint
          name="EmployeeProfile"
          implementation="com.packt.EmployeeProfileImpl"
          url-pattern="/employee"/>
    </endpoints>
  3. Modify the web.xml file as shown:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>JAX-WS-Authentication-Tomcat</display-name>
       <listener>
            <listener-class>
               com.sun.xml.ws.transport.http.servlet.WSServletContextListener
            </listener-class>
        </listener>
        <servlet>
            <servlet-name>employee</servlet-name>
            <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>employee</servlet-name>
            <url-pattern>/employee</url-pattern>
        </servlet-mapping>
       <security-role>
         <description>Normal operator user</description>
         <role-name>operator</role-name>
       	</security-role>
     
    <security-constraint>
          <web-resource-collection>
            <web-resource-name>Operator Roles Security</web-resource-name>
            <url-pattern>/employee</url-pattern>
          </web-resource-collection>
     
          <auth-constraint>
            <role-name>operator</role-name>
          </auth-constraint>
          <user-data-constraint>
              <transport-guarantee>NONE</transport-guarantee>
          </user-data-constraint>
       </security-constraint>
     
    <login-config>
          <auth-method>BASIC</auth-method>
       </login-config>
     
    </web-app>
  4. Authenticate the web services. Edit the tomcat-users.xml file and add this to server.xml:
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase"/>

How it works...

By accessing the following URL, you should be prompted for a login.

Each web service URL is authenticated.

You will be prompted with a login page (http://localhost:8080/EmployeeProfile/employee)

See also

  • The Enabling and disabling the file listing recipe
You have been reading a chapter from
Spring Security 3.x Cookbook
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781782167525
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image