problem with authentication after successfull login it redirecting me to the login page












0















Hello guys after days of researching about my problem none of them works
with me i have a problem with authentication using (Spring) when the user
enter the username and password it redirecting me the login page , can
any one help me please about this situation . thank you.



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
private Environment env;

@Autowired
private UserSecurityService userSecurityService;


private BCryptPasswordEncoder passwordEncoder(){
return SecurityUtility.passwordEncoder();

}

private static final String POBLIC_MATCHERS = {

"/css/**",
"/js/**",
"/images/**",
"/**",
"/newUser",
"/login",
"/resetPassword",
"/Register-form",
"/forgetPassword",
"/products",
"/searchByCategory",
"/searchProduct",
"/productDetail/**"
};



@Override
protected void configure(HttpSecurity http) throws Exception{
http.
authorizeRequests().
antMatchers(POBLIC_MATCHERS).
permitAll().anyRequest().authenticated();

http.csrf().disable().cors().disable()
.formLogin().failureUrl("/loginForm?error=true")
// .defaultSuccessUrl("/")
.loginPage("/loginForm")
.permitAll()
.and().logout().logoutRequestMatcher(new
AntPathRequestMatcher("/logout")).logoutSuccessUrl("/?
logout").deleteCookies("remember-me").permitAll()
.and().rememberMe();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws
Exception{
auth.userDetailsService(userSecurityService)
.passwordEncoder(passwordEncoder());

}



class Controller




@RequestMapping(value="/login")
public String myProfile(
@ModelAttribute("username") String username,
Model model
){
User user =userService.findByUsername(username);

if(user == null){//check if the user exist in database
model.addAttribute("registerError", "Username or
Password incorrect !");
return "loginForm";

} if(user.isEnabled()==false){ //check if the user is enabled
or not
model.addAttribute("messageError", "you have to confirm
your account to continue login !");
return "loginForm";
}

UserDetails principal =
userSecurityService.loadUserByUsername("username");
UsernamePasswordAuthenticationToken authentication = new
UsernamePasswordAuthenticationToken(principal,principal.getPassword(),
principal.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
model.addAttribute("loginError" , "login failed, incorrect
password or username ! ");
System.out.println("success login");

return "myAccount";
}



class UserSecurityService




 @Service
public class UserSecurityService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


static final PasswordEncoder passwordEncoder = new
BCryptPasswordEncoder();
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException{
User user=userRepository.findByUsername(username);

if(user == null){

throw new UsernameNotFoundException("Username not
found");
}

return user;
}

}









share|improve this question

























  • Are you getting any error like wrong username or password??

    – Shubh Dixit
    Jan 19 at 14:05











  • no errors , but it's show me in the console the select query for the specific user

    – S.Amine
    Jan 19 at 14:13











  • try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

    – Shubh Dixit
    Jan 19 at 14:13











  • not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

    – S.Amine
    Jan 19 at 14:25













  • no try passwordEncoder(new BCryptPasswordEncoder())

    – Shubh Dixit
    Jan 19 at 14:26
















0















Hello guys after days of researching about my problem none of them works
with me i have a problem with authentication using (Spring) when the user
enter the username and password it redirecting me the login page , can
any one help me please about this situation . thank you.



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
private Environment env;

@Autowired
private UserSecurityService userSecurityService;


private BCryptPasswordEncoder passwordEncoder(){
return SecurityUtility.passwordEncoder();

}

private static final String POBLIC_MATCHERS = {

"/css/**",
"/js/**",
"/images/**",
"/**",
"/newUser",
"/login",
"/resetPassword",
"/Register-form",
"/forgetPassword",
"/products",
"/searchByCategory",
"/searchProduct",
"/productDetail/**"
};



@Override
protected void configure(HttpSecurity http) throws Exception{
http.
authorizeRequests().
antMatchers(POBLIC_MATCHERS).
permitAll().anyRequest().authenticated();

http.csrf().disable().cors().disable()
.formLogin().failureUrl("/loginForm?error=true")
// .defaultSuccessUrl("/")
.loginPage("/loginForm")
.permitAll()
.and().logout().logoutRequestMatcher(new
AntPathRequestMatcher("/logout")).logoutSuccessUrl("/?
logout").deleteCookies("remember-me").permitAll()
.and().rememberMe();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws
Exception{
auth.userDetailsService(userSecurityService)
.passwordEncoder(passwordEncoder());

}



class Controller




@RequestMapping(value="/login")
public String myProfile(
@ModelAttribute("username") String username,
Model model
){
User user =userService.findByUsername(username);

if(user == null){//check if the user exist in database
model.addAttribute("registerError", "Username or
Password incorrect !");
return "loginForm";

} if(user.isEnabled()==false){ //check if the user is enabled
or not
model.addAttribute("messageError", "you have to confirm
your account to continue login !");
return "loginForm";
}

UserDetails principal =
userSecurityService.loadUserByUsername("username");
UsernamePasswordAuthenticationToken authentication = new
UsernamePasswordAuthenticationToken(principal,principal.getPassword(),
principal.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
model.addAttribute("loginError" , "login failed, incorrect
password or username ! ");
System.out.println("success login");

return "myAccount";
}



class UserSecurityService




 @Service
public class UserSecurityService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


static final PasswordEncoder passwordEncoder = new
BCryptPasswordEncoder();
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException{
User user=userRepository.findByUsername(username);

if(user == null){

throw new UsernameNotFoundException("Username not
found");
}

return user;
}

}









share|improve this question

























  • Are you getting any error like wrong username or password??

    – Shubh Dixit
    Jan 19 at 14:05











  • no errors , but it's show me in the console the select query for the specific user

    – S.Amine
    Jan 19 at 14:13











  • try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

    – Shubh Dixit
    Jan 19 at 14:13











  • not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

    – S.Amine
    Jan 19 at 14:25













  • no try passwordEncoder(new BCryptPasswordEncoder())

    – Shubh Dixit
    Jan 19 at 14:26














0












0








0








Hello guys after days of researching about my problem none of them works
with me i have a problem with authentication using (Spring) when the user
enter the username and password it redirecting me the login page , can
any one help me please about this situation . thank you.



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
private Environment env;

@Autowired
private UserSecurityService userSecurityService;


private BCryptPasswordEncoder passwordEncoder(){
return SecurityUtility.passwordEncoder();

}

private static final String POBLIC_MATCHERS = {

"/css/**",
"/js/**",
"/images/**",
"/**",
"/newUser",
"/login",
"/resetPassword",
"/Register-form",
"/forgetPassword",
"/products",
"/searchByCategory",
"/searchProduct",
"/productDetail/**"
};



@Override
protected void configure(HttpSecurity http) throws Exception{
http.
authorizeRequests().
antMatchers(POBLIC_MATCHERS).
permitAll().anyRequest().authenticated();

http.csrf().disable().cors().disable()
.formLogin().failureUrl("/loginForm?error=true")
// .defaultSuccessUrl("/")
.loginPage("/loginForm")
.permitAll()
.and().logout().logoutRequestMatcher(new
AntPathRequestMatcher("/logout")).logoutSuccessUrl("/?
logout").deleteCookies("remember-me").permitAll()
.and().rememberMe();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws
Exception{
auth.userDetailsService(userSecurityService)
.passwordEncoder(passwordEncoder());

}



class Controller




@RequestMapping(value="/login")
public String myProfile(
@ModelAttribute("username") String username,
Model model
){
User user =userService.findByUsername(username);

if(user == null){//check if the user exist in database
model.addAttribute("registerError", "Username or
Password incorrect !");
return "loginForm";

} if(user.isEnabled()==false){ //check if the user is enabled
or not
model.addAttribute("messageError", "you have to confirm
your account to continue login !");
return "loginForm";
}

UserDetails principal =
userSecurityService.loadUserByUsername("username");
UsernamePasswordAuthenticationToken authentication = new
UsernamePasswordAuthenticationToken(principal,principal.getPassword(),
principal.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
model.addAttribute("loginError" , "login failed, incorrect
password or username ! ");
System.out.println("success login");

return "myAccount";
}



class UserSecurityService




 @Service
public class UserSecurityService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


static final PasswordEncoder passwordEncoder = new
BCryptPasswordEncoder();
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException{
User user=userRepository.findByUsername(username);

if(user == null){

throw new UsernameNotFoundException("Username not
found");
}

return user;
}

}









share|improve this question
















Hello guys after days of researching about my problem none of them works
with me i have a problem with authentication using (Spring) when the user
enter the username and password it redirecting me the login page , can
any one help me please about this situation . thank you.



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
private Environment env;

@Autowired
private UserSecurityService userSecurityService;


private BCryptPasswordEncoder passwordEncoder(){
return SecurityUtility.passwordEncoder();

}

private static final String POBLIC_MATCHERS = {

"/css/**",
"/js/**",
"/images/**",
"/**",
"/newUser",
"/login",
"/resetPassword",
"/Register-form",
"/forgetPassword",
"/products",
"/searchByCategory",
"/searchProduct",
"/productDetail/**"
};



@Override
protected void configure(HttpSecurity http) throws Exception{
http.
authorizeRequests().
antMatchers(POBLIC_MATCHERS).
permitAll().anyRequest().authenticated();

http.csrf().disable().cors().disable()
.formLogin().failureUrl("/loginForm?error=true")
// .defaultSuccessUrl("/")
.loginPage("/loginForm")
.permitAll()
.and().logout().logoutRequestMatcher(new
AntPathRequestMatcher("/logout")).logoutSuccessUrl("/?
logout").deleteCookies("remember-me").permitAll()
.and().rememberMe();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws
Exception{
auth.userDetailsService(userSecurityService)
.passwordEncoder(passwordEncoder());

}



class Controller




@RequestMapping(value="/login")
public String myProfile(
@ModelAttribute("username") String username,
Model model
){
User user =userService.findByUsername(username);

if(user == null){//check if the user exist in database
model.addAttribute("registerError", "Username or
Password incorrect !");
return "loginForm";

} if(user.isEnabled()==false){ //check if the user is enabled
or not
model.addAttribute("messageError", "you have to confirm
your account to continue login !");
return "loginForm";
}

UserDetails principal =
userSecurityService.loadUserByUsername("username");
UsernamePasswordAuthenticationToken authentication = new
UsernamePasswordAuthenticationToken(principal,principal.getPassword(),
principal.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
model.addAttribute("loginError" , "login failed, incorrect
password or username ! ");
System.out.println("success login");

return "myAccount";
}



class UserSecurityService




 @Service
public class UserSecurityService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


static final PasswordEncoder passwordEncoder = new
BCryptPasswordEncoder();
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException{
User user=userRepository.findByUsername(username);

if(user == null){

throw new UsernameNotFoundException("Username not
found");
}

return user;
}

}






java spring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 14:09









Shubh Dixit

1,236418




1,236418










asked Jan 19 at 13:57









S.AmineS.Amine

13




13













  • Are you getting any error like wrong username or password??

    – Shubh Dixit
    Jan 19 at 14:05











  • no errors , but it's show me in the console the select query for the specific user

    – S.Amine
    Jan 19 at 14:13











  • try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

    – Shubh Dixit
    Jan 19 at 14:13











  • not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

    – S.Amine
    Jan 19 at 14:25













  • no try passwordEncoder(new BCryptPasswordEncoder())

    – Shubh Dixit
    Jan 19 at 14:26



















  • Are you getting any error like wrong username or password??

    – Shubh Dixit
    Jan 19 at 14:05











  • no errors , but it's show me in the console the select query for the specific user

    – S.Amine
    Jan 19 at 14:13











  • try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

    – Shubh Dixit
    Jan 19 at 14:13











  • not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

    – S.Amine
    Jan 19 at 14:25













  • no try passwordEncoder(new BCryptPasswordEncoder())

    – Shubh Dixit
    Jan 19 at 14:26

















Are you getting any error like wrong username or password??

– Shubh Dixit
Jan 19 at 14:05





Are you getting any error like wrong username or password??

– Shubh Dixit
Jan 19 at 14:05













no errors , but it's show me in the console the select query for the specific user

– S.Amine
Jan 19 at 14:13





no errors , but it's show me in the console the select query for the specific user

– S.Amine
Jan 19 at 14:13













try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

– Shubh Dixit
Jan 19 at 14:13





try using passwordEncoder(new BCryptPasswordEncoder()) in place of .passwordEncoder(passwordEncoder());

– Shubh Dixit
Jan 19 at 14:13













not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

– S.Amine
Jan 19 at 14:25







not working , i use this .passwordEncoder(passwordEncoder()); from this method private BCryptPasswordEncoder passwordEncoder(){ return SecurityUtility.passwordEncoder(); } did you see it.

– S.Amine
Jan 19 at 14:25















no try passwordEncoder(new BCryptPasswordEncoder())

– Shubh Dixit
Jan 19 at 14:26





no try passwordEncoder(new BCryptPasswordEncoder())

– Shubh Dixit
Jan 19 at 14:26












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267828%2fproblem-with-authentication-after-successfull-login-it-redirecting-me-to-the-log%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267828%2fproblem-with-authentication-after-successfull-login-it-redirecting-me-to-the-log%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre