AuthenticatoinProvider:
Код: Выделить всё
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
final String name = authentication.getName();
final String password = authentication.getCredentials().toString();
if (!"admin".equals(name) || !"system".equals(password)) {
return null;
}
return authenticateAgainstThirdPartyAndGetAuthentication(name, password);
}
@Override
public boolean supports(Class authentication) {
return true;
}
private static UsernamePasswordAuthenticationToken authenticateAgainstThirdPartyAndGetAuthentication(String name, String password) {
final List grantedAuths = new ArrayList();
final UserDetails principal = new User(name, password, grantedAuths);
return new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
}
}
Код: Выделить всё
@Component
public class EverybodyAuthenticationFilter extends OncePerRequestFilter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
SecurityContextHolder.createEmptyContext();
// THIS IS ONLY FOR TESTS. TO GET THIS WORK SENDING HARD CODED TOKEN. THIS SHOULD BE
// CREATED FROM HEADER VALUES.
customAuthenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("admin",
"system"));
filterChain.doFilter(request, response);
}
}
Код: Выделить всё
@EnableWebSecurity
@Configuration
@ComponentScan("com.example.demo")
public class SecurityConfig{
@Autowired
private CustomAuthenticationProvider authProvider;
@Bean
public AuthenticationManager authManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authProvider);
return authenticationManagerBuilder.build();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http,
AuthenticationManager authenticationManager,
EverybodyAuthenticationFilter every) throws Exception {
return http.authorizeHttpRequests(expressionInterceptUrlRegistry ->
expressionInterceptUrlRegistry.anyRequest().authenticated())
.addFilterBefore(every, BasicAuthenticationFilter.class)
.authenticationManager(authenticationManager)
.build();
}
}
Код: Выделить всё
@Controller
public class HomeController {
private static final String HOME_VIEW = "home/index";
@GetMapping("/home")
public String home(HttpServletRequest request){
return HOME_VIEW;
}
}
https://github.com/spring-projects/spri ... sues/12602< /p>
Как заставить это работать?
Подробнее здесь: https://stackoverflow.com/questions/783 ... er-to-work
Мобильная версия