1

i'm try configure Spring Security with Struts 2. I have added all dependencies in pom.xml.

I have configured Spring-database.xml and Spring-security.xml. I have implemeted the model classes for User and Role. I have configured also the DAO Class User.dao and the class for services.

My Login action is the following:

package com.lbv.controllers;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
      public String getUsername() {
            return username;
  }  
  public void setUsername(String username) {
            this.username = username;
        }
        public void setPassword(String password) {
      this.password = password;
  }
  public String getPassword() {
            return password;
  }
    public String execute() {

         System.out.println("je suis dans execute");
         UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println("username: " + userDetails.getUsername());
            System.out.println("password: " + userDetails.getPassword());
            Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) userDetails.getAuthorities();
            for (Iterator it = authorities.iterator(); it.hasNext();) {
                SimpleGrantedAuthority authority = (SimpleGrantedAuthority) it.next();
                System.out.println("Role: " + authority.getAuthority());
            }
                System.out.println("je suis dans execute");  
            return SUCCESS; 
    }

}

and this my athentication form appearing in login page login.jsp:

<s:form name="loginForm" action="j_spring_security_check" method="post">
                            <fieldset>
                                <div class="form-group">
                                    <s:textfield class="form-control" placeholder="Identifiant" name="username"  id="username"/>
                                </div>
                                <div class="form-group">
                                    <s:password class="form-control" placeholder="Mot de passe" name="password" id="password" />
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input name="remember" type="checkbox" value="Remember Me">Se souvenir de moi
                                    </label>
                                </div>
                                <!-- Change this to a button or input when using this as a form -->
                                <s:submit class="btn btn-lg btn-success btn-block" value="Se connecter"/>

                            </fieldset>
                        </s:form>

When i run my project, the login page appear, but when enter a valid username and password. I get the following error:

ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'password' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'password' with value ['smail', ]
ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'username' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'username' with value ['smail', ]
Roman C
  • 49,761
  • 33
  • 66
  • 176

0 Answers0