Friday, May 15, 2015

Srping Security -To integrate Spring Security in a Web Project


Sumary:
Spring Security gives you a feature to intercept any URL authorize and authenticate.
Load a Security configuration to context ,intercept the url through Filter and it shall lead you to a default login page

11.)    Include below dependency to your pom – org.springframework.security and org.springframework.security

<dependency>
    <
groupId>org.springframework.security</groupId>
    <
artifactId>spring-security-web</artifactId>
    <
version>${spring.security.version}</version>
</
dependency>

<
dependency>
    <
groupId>org.springframework.security</groupId>
    <
artifactId> org.springframework.security </artifactId>
    <
version>4.0.1.RELEASE</version>
</
dependency>

22.)    Create a spring-security.xml
<beans:beans xmlns="http://www.springframework.org/schema/security"

                           xmlns:beans="http://www.springframework.org/schema/beans"

                           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                           xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

   http://www.springframework.org/schema/security

   http://www.springframework.org/schema/security/spring-security-4.0.xsd">



<http auto-config="true" use-expressions="true">

    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

</http>



<authentication-manager>

    <authentication-provider>

        <user-service>

            <user name="user" password="user" authorities="ROLE_USER" />

        </user-service>

    </authentication-provider>

</authentication-manager>



</beans:bean
 
33.)    Put below Spring Filter to web.xml –
<!-- Spring Security -->

<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>


44.)    Import Spring security in the ApplicationContext file 
.
@ImportResource({"/com/sapan/context/spring-security.xml"})  
 
Or  load the context through web.xml

<!-- Loads Spring Security config file -->
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                       /WEB-INF/spring-security.xml
                </param-value>
        </context-param>




http://localhost:8081/ --will lead you to the below default login page, 

Note : you may create yyour own custom login page too
For any Queries write to - jsapan4@gmail.com











No comments:

Post a Comment