Pages

Showing posts with label authentication. Show all posts
Showing posts with label authentication. Show all posts

Friday, December 11, 2015

Spring Security with Md5 password encoder Authentication


Spring Security to a web application with md5 password encoder


  1. Required Maven Libraries:
  2.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-config</artifactId>
    </dependency>
    
  3. Configuration files:
    1. MD5 Password Encoder script(md5encoder.sh)
    2. 1
      2
      3
      4
      5
      6
      7
      #!/bin/bash
      ##########################################################################
      # Name  : MD5 Password Encoder for PRISM API 
      ##########################################################################
      echo "Please enter password to be encoded:"
      read md5pass
      echo -n $md5pass | md5sum | awk '{print $1}'
      
    3. User Profiles(profiles.properties)
    4. 1
      2
      3
      # Basic Authentication credentials in APP
      # Format  is <username> = <md5encodedpassword>,<userRole>,<isUserEnabled> 
      candy=5f4dcc3b5aa765d61d8327deb882cf99,ROLE_USER,enabled
      
    5. Spring context xml(security-app-context.xml)
    6.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                 http://www.springframework.org/schema/security
                 http://www.springframework.org/schema/security/spring-security-3.1.xsd">
      
       <security:http entry-point-ref="authenticationEntryPoint"
        use-expressions="true">
        <security:intercept-url pattern="/**"  access="hasAnyRole ( 'ROLE_USER')" />
         <security:logout invalidate-session="true" delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" />
        <security:custom-filter ref="basicAuthenticationFilter" position="BASIC_AUTH_FILTER" />
       </security:http>
       
       <bean id="basicAuthenticationFilter"
        class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
        <property name="authenticationManager" ref="authManager" />
        <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
       </bean>
       <bean id="authenticationEntryPoint"
        class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
        <property name="realmName" value="PRISM" />
       </bean>
      
       <bean id="md5encoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder">
       </bean>
       <security:authentication-manager alias="authManager"> 
        <security:authentication-provider>
            <security:password-encoder ref="md5encoder" />
         <security:user-service id="userDetailsService" properties="file:{path}/profiles.properties"/>
        </security:authentication-provider>
       </security:authentication-manager>
      </beans>
      
    7. Web application xml(web.xml)
    8.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>file:{path}/security-app-context.xml</param-value>
      </context-param>
      <!-- security start -->
      <filter>
       <filter-name>springSecurityFilterChain</filter-name>
       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>
      <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
      <!-- security end -->