problem with authentication after successfull login it redirecting me to the login page
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
|
show 3 more comments
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
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 usingpasswordEncoder(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 trypasswordEncoder(new BCryptPasswordEncoder())
– Shubh Dixit
Jan 19 at 14:26
|
show 3 more comments
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
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
java spring
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 usingpasswordEncoder(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 trypasswordEncoder(new BCryptPasswordEncoder())
– Shubh Dixit
Jan 19 at 14:26
|
show 3 more comments
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 usingpasswordEncoder(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 trypasswordEncoder(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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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