Boa tarde pessoal .
estou criando uma app springSecurity e nao esta validando a senha e usuario de autenticação simples.Nao esta validando o usuario e senha
Veja minhas classes
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", “/telaPrincipal”).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Override
public void configure(WebSecurity web) throws Exception{
web.ignoring().antMatchers("/img/**", "/css/**");
}
MvcConfig
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
//registry.addViewController("/home").setViewName("home");
// registry.addViewController("/").setViewName("home");
registry.addViewController("/Telaprincipal").setViewName("telaPrincipal");
registry.addViewController("/login").setViewName("login");
}
html
Usuario ou senha invalidos.
You have been logged out.
thimor
Julho 24, 2020, 4:59pm
#2
voce colocou o usuario e senha no arquivo application.properties?
spring.security.user.name=joao
spring.security.user.password=secreta
?
@EnableWebSecurity
@ComponentScan(basePackageClasses = TokenAuthenticationService.class)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors();
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/", “/telaPrincipal”).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
.and()
.httpBasic();
}
}
testa ai assim
thimor:
@EnableWebSecurity @ComponentScan (basePackageClasses = TokenAuthenticationService.class) @EnableGlobalMethodSecurity (prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http.cors(); http .csrf().disable() .authorizeRequests() .antMatchers("/", “/telaPrincipal”).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); .and() .httpBasic(); } }
tem que criar esta classe TokenAuthenticationService?
Desconsiderei este @ComponentScan (basePackageClasses = TokenAuthenticationService.class)
para testar e nao funcionou da ainda erro de senha e usuario e nao direciona pra telaPrincipal.html
thimor
Julho 24, 2020, 6:05pm
#5
isso, eh que eu copiei da minha classe e nao apaguei. Voce definiu um bean de password enconding?
@bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoding.getInstance();
}
Não , esse metodo vai na classe do SecurityConfig mesmo ?
rbbasilio:
NoOpPasswordEncoding
este NoOpPasswordEncoding esta dano erro pra mim onde busco isto?
olha como ta a classe agora
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity (prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors();
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/", “/telaPrincipal”).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("sys")
.password("123")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/img/**", "/css/**");
}
}
thimor
Julho 24, 2020, 6:42pm
#9
rbbasilio:
NoOpPasswordEncoding
ta errado o nome da classe
NoOpPasswordEncoder
quando clica no botao da o erro de mensagem que coloquei de usurio invalido e fica apenas na tela logim