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











Wednesday, May 6, 2015

Unit Testing – ( Junit --( http://junit.org/ ) & Mockito -- (http://site.mockito.org/#why) )

Unit Testing – ( Junit  --(  http://junit.org/  ) & Mockito  -- (http://site.mockito.org/#why) )

JUnit is a framework for writing and running  Unit tests
Mockito allows to create mocks (test doubles) which can replace real collaborators of a class, thus making it possible to implement some test cases.

JUnit and Mockito are often used together. JUnit is used to create the structure of test, while Mockito allow to specify some details of tests within the framework imposed by JUnit. 

I’d be covering an example of Mockito with Junit, (pre-requisite—Junit basics)
Junit Theory:
@Test – Designate a test
@Before/@After – blocks execute before/After
@setup—setup methods executes before a test methods
@tear – executes after test methods

Mockito Theory:
@Mock / mock() – This creates a dummy object , in short mock up an object if you don’t want to fetch any potential value out of it or methods returning null or zero on each call
Ex- Below class will always be mocked
@Repository
public class OrderDao {
    public Order getOrder(int irderId){
        throw new UnsupportedOperationException("Fail is not mocked!");
    }
}
@InjectMocks – Creates an instance
@Spy/ spy() – Used to mock object which fetch some real value
Ex—
@Service
public class PriceService {
    public int getActualPrice(Item item){
        throw new UnsupportedOperationException("Fail is not mocked!");
    }

    public int calculatePriceForOrder(Order order){
        int orderPrice = 0;
        for (Item item : order.getItems()){
            orderPrice += getActualPrice(item);
        }
        return orderPrice;
    }
}


MockitoAnnotations.initMocks(this)) --to initialize annotated fields. Without this call, these objects would be null. Common mistake with this approach is to forget this invocation.
If you have Any Annotated class – like Autowired , above method has to be called otherwise these objects would be null


I'd be covering example in my next blog

for any queries , please contact - jsapan4@gmail.com