0

I need to read a property value in my UserDetailsDaoImpl. I'm using Spring Security.

It succesfully reads inside of a @Controller but not in this class maybe because it's a @Repository.

What can i do to read the property value?

UserDetailsDaoImpl:

@Repository
public class UserDetailsDaoImpl extends JdbcDaoSupport implements UserDetailsDao {

    @Value("${emails_blocked}")
    private String emails_blocked;

Beans:

<context:property-placeholder location="classpath:config.properties"/>

edit:

this is how i call UserDetailsDaoImpl:

@Autowired
UserDetailsDao userDetailsDao;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    try {

        Authentication auth = super.authenticate(authentication);

        // if reach here, means login success, else exception will be thrown
        // reset the user_attempts
        userDetailsDao.resetFailAttempts(authentication.getName());

        return auth;

    } catch (BadCredentialsException e) {

        userDetailsDao.updateFailAttempts(authentication.getName());
        throw e;

    }

my beans updated:

<beans:bean id="userDetailsDao" class="com.setelog.spring.dao.UserDetailsDaoImpl" >
    <beans:property name="dataSource" ref="dataSource" />
</beans:bean>

<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>


<beans:bean id="authenticationProvider"
    class="com.setelog.spring.handler.LimitLoginAuthenticationProvider">
    <beans:property name="userDetailsService" ref="customUserDetailsService" />
    <beans:property name="userDetailsDao" ref="userDetailsDao" />
    <beans:property name="passwordEncoder" ref="encoder" />

</beans:bean>
Kunal
  • 327
  • 1
  • 4
  • 19
  • 1
    Make sure you aren't creating a new instance yourself and that you also have a `` in the context where the `@Repository` is in. If not nothing will be replaced. – M. Deinum May 28 '15 at 11:28
  • @M.Deinum can you please give me an example? I did not understand. Thanks – Kunal May 28 '15 at 12:07
  • What is it you don't understand? Check the place where yu use this controller you aren't creating a new instance yourself, and make sure that you have the placeholder configured in the application context the repository is defined/loaded in. – M. Deinum May 28 '15 at 12:25
  • I have updated my question with how i call it. I also added an autowire and checked the placeholder but still the same – Kunal May 28 '15 at 13:43
  • Then you don't have `` in that configuration file and hence your `@Value` is basically useless. Add it. – M. Deinum May 28 '15 at 13:46
  • I have it. Its is the mvc-dispatcher.servlet.xml. I can read the file succesfully in controllers the only problem is when i try to read them form this particular class. I think it is because i am calling userDetailsDao.updateFailAttempts(authentication.getName()); and beause of that it does not load the @Value.... – Kunal May 28 '15 at 14:05
  • You have it in the wrong context. You repository is in the root context and that doesn't have the ``, currently only your beans loaded by the `DispatcherServlet` are enhanced not the ones loaded by the `ContextLoaderListener`. – M. Deinum May 29 '15 at 05:28

1 Answers1

-1

My Problem was that since i called a method it wouldn't load the @Value so i had to inject some beans. Like this:

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>            
            </list>
        </property>
    </bean>

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.setelog.spring.dao.UserDetailsDaoImpl.setEmails_Blocked"/>
        <property name="arguments">
            <list>
                <value>${emails_blocked}</value>
            </list>
       </property>
    </bean>

my UserDetailsDaoImpl:

static String emails_blocked;

public static void setEmails_Blocked(String emails_blocked){
    UserDetailsDaoImpl.emails_blocked= emails_blocked;
}

This Answer helped me a lot: https://stackoverflow.com/a/24649692/4790786

Community
  • 1
  • 1
Kunal
  • 327
  • 1
  • 4
  • 19
  • No you don't you just need to specify `` in the correct context. It will only operate on beans in the same context, in your case only beans loaded by the `DispatcherServlet`, add the line to the `ContextLoaderListener` as well. – M. Deinum May 29 '15 at 05:29