You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Kachibnb <no...@gmail.com> on 2015/07/26 10:22:07 UTC

Wicket session how to get data using spring bean

I have this Spring bean.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd ">
    <bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url"
value="jdbc:mysql://localhost:3306/fueltopupnigeria"/>
		<property name="username" value="root"/>
		<property name="password" value="nbuser"/>
	</bean>
 
    
<security:authentication-manager alias ="authenticationManager">
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select Username,Password from registration where
username = ? and password =?"/>
</security:authentication-provider>
</security:authentication-manager>
 
<security:global-method-security secured-annotations="enabled" />
	  
 
</beans>
 this wicket web session
import org.apache.log4j.Logger;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.request.Request;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.authentication.AuthenticationManager;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
 
/**
 *
 * @author Uche
 */
public class Fuelwebsession extends AuthenticatedWebSession {
 
private static final long serialVersionUID = 3355101222374558750L;
 
private static final Logger logger = Logger.getLogger(Fuelwebsession.class);
 
@SpringBean(name = "authenticationManager")
private AuthenticationManager authenticationManager;
 
public Fuelwebsession(Request request) {
super(request);
injectDependencies();
ensureDependenciesNotNull();
}
 
private void ensureDependenciesNotNull() {
if (authenticationManager == null) {
throw new IllegalStateException("An authenticationManager is required.");
}
}
 
private void injectDependencies() {
Injector.get().inject(this);
}
 
@Override
public boolean authenticate(String username, String password) {
boolean authenticated = false;
try {
Authentication authentication = authenticationManager.authenticate(new
UsernamePasswordAuthenticationToken(username, password));
SecurityContextHolder.getContext().setAuthentication(authentication);
authenticated = authentication.isAuthenticated();
} catch (AuthenticationException e) {
logger.warn(String.format("User &#039;%s&#039; failed to login. Reason: %s",
username, e.getMessage()));
authenticated = false;
}
return authenticated;
}
 
@Override
public Roles getRoles() {
Roles roles = new Roles();
getRolesIfSignedIn(roles);
return roles;
}
 
private void getRolesIfSignedIn(Roles roles) {
if (isSignedIn()) {
Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
addRolesFromAuthentication(roles, authentication);
}
}
 
private void addRolesFromAuthentication(Roles roles, Authentication
authentication) {
for (GrantedAuthority authority : authentication.getAuthorities()) {
roles.add(authority.getAuthority());
}
}
}
 and this LoginPage
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
 
public class LoginPage extends BasePage {
 
    public LoginPage() {
	final LoginForm form = new LoginForm("loginForm");
	add(form);
 
    }
 
    private static class LoginForm extends StatelessForm {
 
	private static final long serialVersionUID = -6826853507535977683L;
 
	private String username;
	private String password;
 
	public LoginForm(String id) {
	    super(id);
	    setModel(new CompoundPropertyModel(this));
	    add(new Label("usernameLabel", getString("login.username.label", null,
"username")));
	    add(new RequiredTextField("username"));
	    add(new Label("passwordLabel", getString("login.password.label", null,
"password")));
	    add(new PasswordTextField("password"));
	    add(new FeedbackPanel("feedback"));
 
	}
 
	@Override
	protected void onSubmit() {
	    AuthenticatedWebSession session = AuthenticatedWebSession.get();
          
	    if (session.signIn(username, password)) {
		setDefaultResponsePageIfNecessary();
	    } else {
		setResponsePage(ErrorPage.class);
	    }
	}
 
	private void setDefaultResponsePageIfNecessary() {
	    if (!continueToOriginalDestination()) {
		setResponsePage(Welcome.class);
            }
                else{
                setResponsePage(Dealersession.class);
            }
            if (!continueToOriginalDestination()) {}
            else{
              setResponsePage(Cooperatesession.class);
	    }
	}
    
    }
}
But it gives an error showing the login page cannot authenticate. I m using
my own error page so what is wrong with the codes above. I stuck and need
help urgently. 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-session-how-to-get-data-using-spring-bean-tp4671656.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket session how to get data using spring bean

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

What exactly is the error? It is not very clear from your description.

Martin Grigorov
Freelancer. Available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Sun, Jul 26, 2015 at 11:22 AM, Kachibnb <no...@gmail.com> wrote:

> I have this Spring bean.
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>        xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
> xmlns:security="http://www.springframework.org/schema/security"
> xsi:schemaLocation="http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
> http://www.springframework.org/schema/security
> http://www.springframework.org/schema/security/spring-security-3.0.xsd ">
>     <bean id="dataSource"
>
> class="org.springframework.jdbc.datasource.DriverManagerDataSource">
>                 <property name="driverClassName"
> value="com.mysql.jdbc.Driver"/>
>                 <property name="url"
> value="jdbc:mysql://localhost:3306/fueltopupnigeria"/>
>                 <property name="username" value="root"/>
>                 <property name="password" value="nbuser"/>
>         </bean>
>
>
> <security:authentication-manager alias ="authenticationManager">
> <security:authentication-provider>
> <security:jdbc-user-service data-source-ref="dataSource"
> users-by-username-query="select Username,Password from registration where
> username = ? and password =?"/>
> </security:authentication-provider>
> </security:authentication-manager>
>
> <security:global-method-security secured-annotations="enabled" />
>
>
> </beans>
>  this wicket web session
> import org.apache.log4j.Logger;
> import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
> import org.apache.wicket.authroles.authorization.strategies.role.Roles;
> import org.apache.wicket.injection.Injector;
> import org.apache.wicket.request.Request;
> import org.apache.wicket.spring.injection.annot.SpringBean;
> import org.springframework.security.authentication.AuthenticationManager;
> import
>
> org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
> import org.springframework.security.core.Authentication;
> import org.springframework.security.core.AuthenticationException;
> import org.springframework.security.core.GrantedAuthority;
> import org.springframework.security.core.context.SecurityContextHolder;
>
> /**
>  *
>  * @author Uche
>  */
> public class Fuelwebsession extends AuthenticatedWebSession {
>
> private static final long serialVersionUID = 3355101222374558750L;
>
> private static final Logger logger =
> Logger.getLogger(Fuelwebsession.class);
>
> @SpringBean(name = "authenticationManager")
> private AuthenticationManager authenticationManager;
>
> public Fuelwebsession(Request request) {
> super(request);
> injectDependencies();
> ensureDependenciesNotNull();
> }
>
> private void ensureDependenciesNotNull() {
> if (authenticationManager == null) {
> throw new IllegalStateException("An authenticationManager is required.");
> }
> }
>
> private void injectDependencies() {
> Injector.get().inject(this);
> }
>
> @Override
> public boolean authenticate(String username, String password) {
> boolean authenticated = false;
> try {
> Authentication authentication = authenticationManager.authenticate(new
> UsernamePasswordAuthenticationToken(username, password));
> SecurityContextHolder.getContext().setAuthentication(authentication);
> authenticated = authentication.isAuthenticated();
> } catch (AuthenticationException e) {
> logger.warn(String.format("User &#039;%s&#039; failed to login. Reason:
> %s",
> username, e.getMessage()));
> authenticated = false;
> }
> return authenticated;
> }
>
> @Override
> public Roles getRoles() {
> Roles roles = new Roles();
> getRolesIfSignedIn(roles);
> return roles;
> }
>
> private void getRolesIfSignedIn(Roles roles) {
> if (isSignedIn()) {
> Authentication authentication =
> SecurityContextHolder.getContext().getAuthentication();
> addRolesFromAuthentication(roles, authentication);
> }
> }
>
> private void addRolesFromAuthentication(Roles roles, Authentication
> authentication) {
> for (GrantedAuthority authority : authentication.getAuthorities()) {
> roles.add(authority.getAuthority());
> }
> }
> }
>  and this LoginPage
> import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.markup.html.form.PasswordTextField;
> import org.apache.wicket.markup.html.form.RequiredTextField;
> import org.apache.wicket.markup.html.form.StatelessForm;
> import org.apache.wicket.markup.html.panel.FeedbackPanel;
> import org.apache.wicket.model.CompoundPropertyModel;
>
> public class LoginPage extends BasePage {
>
>     public LoginPage() {
>         final LoginForm form = new LoginForm("loginForm");
>         add(form);
>
>     }
>
>     private static class LoginForm extends StatelessForm {
>
>         private static final long serialVersionUID = -6826853507535977683L;
>
>         private String username;
>         private String password;
>
>         public LoginForm(String id) {
>             super(id);
>             setModel(new CompoundPropertyModel(this));
>             add(new Label("usernameLabel",
> getString("login.username.label", null,
> "username")));
>             add(new RequiredTextField("username"));
>             add(new Label("passwordLabel",
> getString("login.password.label", null,
> "password")));
>             add(new PasswordTextField("password"));
>             add(new FeedbackPanel("feedback"));
>
>         }
>
>         @Override
>         protected void onSubmit() {
>             AuthenticatedWebSession session =
> AuthenticatedWebSession.get();
>
>             if (session.signIn(username, password)) {
>                 setDefaultResponsePageIfNecessary();
>             } else {
>                 setResponsePage(ErrorPage.class);
>             }
>         }
>
>         private void setDefaultResponsePageIfNecessary() {
>             if (!continueToOriginalDestination()) {
>                 setResponsePage(Welcome.class);
>             }
>                 else{
>                 setResponsePage(Dealersession.class);
>             }
>             if (!continueToOriginalDestination()) {}
>             else{
>               setResponsePage(Cooperatesession.class);
>             }
>         }
>
>     }
> }
> But it gives an error showing the login page cannot authenticate. I m using
> my own error page so what is wrong with the codes above. I stuck and need
> help urgently.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Wicket-session-how-to-get-data-using-spring-bean-tp4671656.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>