Answer the question
In order to leave comments, you need to log in
What is the problem with authentication through Spring?
Hello, I can’t understand what the problem is, I created an application that registers the user, everything is correct in this part, the user is really entered into the database and his role appears in the role table, but when it comes to authentication, nothing happens, and even in the application console writes nothing (as in the case of registration, I at least see that the application made a request to the database and put the user there), in the case of authentication, nothing, no error, no result, please help, here is a link to the git: https://github.com /DennisKingsman/NetCracker
if it's convenient I can move all the code here, thanks in advance
desired behavior: after I added a user to the database on the registration page, I have to log in on the login page so that when I try to go to the personalAccount page, I will no longer be redirected to the login
code:
here is the web protection controller, which seems to find the user by his name and then finds his role and authenticates him, probably this thing does not work because I did not see any request in the application console, while when I put the user in the database I see this request
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home","/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.usersByUsernameQuery("select username, password, active from usr where username = ? ")
.authoritiesByUsernameQuery("select u.username, ur.roles from usr u inner join user_role ur on u.id = ur.user_id where u.username = ? "); //take name and role of user
}
}
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
@RequestMapping(value = "/personalAccount", method = RequestMethod.GET)
public String personalAccount(Model model, Principal principal){
String userInfo = principal.getName();
model.addAttribute("userInfo", userInfo);
return "personalAccount";
}
Answer the question
In order to leave comments, you need to log in
Given that there is a ready-made video that matches your code and authentication method via jdbcAuthentication() one to one, I recommend watching:
https://www.youtube.com/watch?v=WDlifgLS8iQ
and you can even one to one reproduce for yourself.
As for your code, then:
1) add to application.properties
logging.level.org.springframework.security=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type=TRACE
2019-05-25 20:20:12.446 DEBUG 21154 --- [nio-8080-exec-2] o.s.s.p.JdbcUserDetailsManager : Query returned no results for user ''
2019-05-25 20:20:12.450 DEBUG 21154 --- [nio-8080-exec-2] o.s.s.a.dao.DaoAuthenticationProvider : User '' not found
org.springframework.security.authentication.BadCredentialsException: Bad credentials
.formLogin()
.loginPage("/login")
.usernameParameter("name")
.passwordParameter("password")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question