Announcement Announcement Module
Collapse
No announcement yet.
SecureContext can never be initialized! Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SecureContext can never be initialized!

    Hi,

    I've been struggling for making Acegi (coupled with Spring) work in a dummy main() method, outside of a Web environment...

    Impossible to make a SecureContext created and all the help concern only the Web part, that I don't want to use.

    Here my method main

    public static void main(String[] args) {
    try {
    XmlBeanFactory beanFactory = initRealComponent();
    UsernamePasswordAuthenticationToken userPassAuthToken = new UsernamePasswordAuthenticationToken("marissa","koa la");

    AuthenticationManager authManager = (AuthenticationManager) beanFactory.getBean("authenticationManager");
    authManager.authenticate(userPassAuthToken);

    DummyMethod dummyMethod = (DummyMethod)beanFactory.getBean("dummyBean");
    dummyMethod.executeDummy("Toto");
    } catch (AuthenticationException e) {
    System.out.println("Auth Error");
    e.printStackTrace();
    } catch (BeansException e) {
    System.out.println("BeansException");
    e.printStackTrace();
    }
    }


    <bean id="dummySecurityInterceptor"
    class="net.sf.acegisecurity.intercept.method.aopal liance.MethodSecurityInterceptor">
    <property name="validateConfigAttributes"><value>true</value></property>
    <property name="authenticationManager"><ref bean="authenticationManager"/></property>
    <property name="accessDecisionManager"><ref bean="accessDecisionManager"/></property>

    <!-- ## Changement du Authentication -->
    <property name="runAsManager"><ref bean="runAsManager"/></property>

    <!-- ## Apply to method signature -->
    <property name="objectDefinitionSource">
    <value>
    com.decalog.security.DummyToSecure.*=ROLE_SUPERVIS OR
    com.decalog.security.DummyMethod.executeDummy=ROLE _SUPERVISOR
    </value>
    </property>
    </bean>

    <bean id="dummyTarget" class="com.decalog.security.DummyToSecure">
    </bean>

    <bean id="dummyBean" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces"><value>com.decalog.security .DummyMethod</value></property>
    <property name="target"><ref bean="dummyTarget"/></property>
    <property name="interceptorNames">
    <list>
    <value>dummySecurityInterceptor</value>
    </list>
    </property>
    </bean>



    public class DummyToSecure implements DummyMethod {
    public void executeDummy(String message) {
    System.out.println("Dummy message:"+message);
    }
    }


    Thank you for any help!!

  • #2
    What do you mean by "Impossible to make a SecureContext created" ?

    Comment


    • #3
      First of all, I would encourage you to look at the Contact Sample application's ClientApplication.java to Acegi Security use in a simple console-based app.

      The problem with your code would appear:

      Code:
      authManager.authenticate&#40;userPassAuthToken&#41;;
      Try:

      Code:
      Authentication auth;
      try &#123;
        auth = authManager.authenticate&#40;userPassAuthToken&#41;;
      &#125; catch &#40;AuthenticationException fail&#41; &#123;
        // do something
      &#125;
      
      ContextHolder.setContext&#40;new SecureContextImpl&#40;&#41;&#41;;
      &#40;&#40;SecureContext&#41;ContextHolder.getContext&#40;&#41;&#41;.setAuthentication&#40;auth&#41;;
      
      // your code here....

      Comment

      Working...
      X