Announcement Announcement Module
Collapse
No announcement yet.
Not able to implement spring security properly Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not able to implement spring security properly

    I have implemented spring security but encountering some stupid problem.Whenever i tried to login with userid and password ,it gives me error The requested resource (/HRMSSpringApp/) is not available.HRMSSpringApp is my application's name.But when i click on browser's back button and again try to login with the same credentials it works fine.
    My applicationContext-security.xml file is as follows:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <b:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:b="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-2.5.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    <http auto-config="true">
            <intercept-url pattern ="/login.jsp*" access = "IS_AUTHENTICATED_ANONYMOUSLY" />
            <intercept-url pattern ="/**" access = "ROLE_USER" />
            <form-login   login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" default-target-url="/index.htm"/>
            <logout logout-success-url="/login.jsp"/>
        </http>
       <b:bean id="customUserDetailsService" class="service.UserServiceDetailsImpl">
        <b:property name="dataSource" ref="dataSource" />
       </b:bean>
     <authentication-provider user-service-ref="customUserDetailsService" >
      </authentication-provider>
    </b:beans>
    My UserServiceDetailsImpl is as follows:
    Code:
    package service;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.sql.DataSource;
    import org.springframework.dao.DataAccessException;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
    import org.springframework.security.GrantedAuthority;
    import org.springframework.security.GrantedAuthorityImpl;
    import org.springframework.security.userdetails.UserDetails;
    import org.springframework.security.userdetails.User;
    import org.springframework.security.userdetails.UserDetailsService;
    import org.springframework.security.userdetails.UsernameNotFoundException;
    
    
    
    
    /**
     *
     * @author dheeraja
     */
    public class UserServiceDetailsImpl implements UserDetailsService {
        private DataSource  dataSource;
    
        public void setDataSource(DataSource dataSource){
            this.dataSource = dataSource;
        }
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
           String sql = "select UM1.EMP_ID,UM1.EMP_PASSWORD,UM1.ACCOUNT_STATUS,URM1.ROLE_ID,URM1.IS_DEFAULT_ROLE from USER_MAST1 UM1 INNER JOIN USER_ROLE_MAPPING1 URM1 ON UM1.EMP_ID = URM1.EMP_ID  where UM1.EMP_ID = :username";
            MapSqlParameterSource source = new MapSqlParameterSource();
            source.addValue("username", username);
            SimpleJdbcTemplate sjt = new SimpleJdbcTemplate(dataSource);
            User user = sjt.queryForObject(sql, new UserMapper(), source);
            return user;
        }
    
        private class UserMapper implements ParameterizedRowMapper<User> {
             @Override
             public UserInfoService mapRow(ResultSet rs, int arg1) throws SQLException {
                 return new UserInfoService(rs.getString("EMP_ID"), rs.getString("EMP_PASSWORD"), true, true, true, true, new GrantedAuthority[]{new GrantedAuthorityImpl("ROLE_USER")},rs.getString("ROLE_ID"),rs.getString("IS_DEFAULT_ROLE"));
             }
         }
    }
    If anybody can please help then it would be gr8.
Working...
X