-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: atualização versão spring e refactor #64
- Loading branch information
1 parent
d85a881
commit 0a9ba96
Showing
28 changed files
with
305 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 37 additions & 54 deletions
91
src/main/java/br/ufrn/dct/apf/configuration/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,64 @@ | ||
package br.ufrn.dct.apf.configuration; | ||
|
||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.builders.WebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | ||
public class SecurityConfiguration { | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
@Value("${spring.queries.users-query}") | ||
private String usersQuery; | ||
|
||
@Value("${spring.queries.roles-query}") | ||
private String rolesQuery; | ||
|
||
@Override | ||
protected void configure(AuthenticationManagerBuilder auth) | ||
throws Exception { | ||
auth. | ||
jdbcAuthentication() | ||
.usersByUsernameQuery(usersQuery) | ||
.authoritiesByUsernameQuery(rolesQuery) | ||
.dataSource(dataSource) | ||
.passwordEncoder(passwordEncoder()); | ||
} | ||
private UserDetailsService userDetailsService; | ||
|
||
@Bean | ||
public BCryptPasswordEncoder passwordEncoder() { | ||
BCryptPasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public AuthenticationManager authenticationManagerBean() throws Exception { | ||
return super.authenticationManagerBean(); | ||
ModelMapper modelMapper() { | ||
return new ModelMapper(); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
|
||
http. | ||
authorizeRequests() | ||
.antMatchers("/").permitAll() | ||
.antMatchers("/login").permitAll() | ||
.antMatchers("/registration").permitAll() | ||
.antMatchers("/admin/**").hasAuthority("ADMIN") | ||
.antMatchers("/home").hasAuthority("USER").anyRequest() | ||
.authenticated() | ||
.and().csrf().disable() | ||
.formLogin().loginPage("/login").failureUrl("/login?error=true") | ||
.defaultSuccessUrl("/home") | ||
.usernameParameter("email") | ||
.passwordParameter("password") | ||
.and().logout() | ||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) | ||
.logoutSuccessUrl("/").and().exceptionHandling() | ||
.accessDeniedPage("/access-denied"); | ||
|
||
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
auth | ||
.userDetailsService(userDetailsService) | ||
.passwordEncoder(passwordEncoder()); | ||
} | ||
|
||
@Override | ||
public void configure(WebSecurity web) throws Exception { | ||
web | ||
.ignoring() | ||
.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**"); | ||
@Bean | ||
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http. | ||
authorizeHttpRequests(requests -> | ||
requests | ||
.requestMatchers("/").permitAll() | ||
.requestMatchers("/login").permitAll() | ||
.requestMatchers("/registration").permitAll() | ||
.requestMatchers("/admin/**").hasAuthority("ADMIN") | ||
.requestMatchers("/home").hasAuthority("USER").anyRequest() | ||
.authenticated() | ||
).formLogin(login -> | ||
login | ||
.loginPage("/login") | ||
.failureUrl("/login?error=true") | ||
.defaultSuccessUrl("/home") | ||
.usernameParameter("email") | ||
.passwordParameter("password") | ||
).logout(logout -> | ||
logout | ||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) | ||
.logoutSuccessUrl("/")).exceptionHandling(handling -> handling | ||
.accessDeniedPage("/access-denied")); | ||
return http.build(); | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
src/main/java/br/ufrn/dct/apf/configuration/WebMvcConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.