Код: Выделить всё
org.springframework.boot
spring-boot-starter-oauth2-client
Код: Выделить всё
spring:
application:
name: MyTestApp
security:
oauth2:
client:
registration:
github:
clientId: 983rkjhsaf809faj3
clientSecret: 0af98s09gdf8sj3l5rjadsf98a7fdvdsaf
Пока это хорошо, но, возможно, корневой каталог http://localhost:8080, который является "/", также должен просматриваться пользователями, не вошедшими в систему. Отправка их на страницу входа в GitHub будет. ошибиться. Поэтому в статье предлагается расширить код с помощью класса WebSecurityConfigurerAdapter, и этот класс больше не существует для текущей версии Spring Security 6.4.1. Полагаю, показанный пример относится к Spring Security 5 (?).
Поэтому я попытался добавить конфигурацию безопасности новым способом, используя SecurityFilterChain.
Код: Выделить всё
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/", "/error", "/webjars/**").permitAll()
.anyRequest().authenticated()
)
.exceptionHandling(e -> e
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
)
.oauth2Login((oauth2Login) -> oauth2Login
.userInfoEndpoint((userInfo) -> userInfo
.userAuthoritiesMapper(grantedAuthoritiesMapper())
)
);
return http.build();
}
private GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
return (authorities) -> {
Set mappedAuthorities = new HashSet();
authorities.forEach((authority) -> {
GrantedAuthority mappedAuthority;
if (authority instanceof OAuth2UserAuthority) {
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) authority;
mappedAuthority = new OAuth2UserAuthority("OAUTH2_USER", userAuthority.getAttributes());
} else {
mappedAuthority = authority;
}
mappedAuthorities.add(mappedAuthority);
});
return mappedAuthorities;
};
}
}
То, чего я хочу добиться, очень просто:
- Я хочу, чтобы пользователи всегда могли позвонить по адресу http://localhost:8080 (следовательно, указывая на «/») независимо от того, вошли они в систему или нет. .
- Если пользователи называют какой-либо URL другим чем те, которые определены для PermitAll(), я хочу, чтобы они получили HTTP ERROR 401.
Подробнее здесь: https://stackoverflow.com/questions/792 ... 2-redirect
Мобильная версия