1package com.frugalis;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.springframework.security.authentication.AuthenticationProvider;
7import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
8import org.springframework.security.core.Authentication;
9import org.springframework.security.core.AuthenticationException;
10import org.springframework.security.core.GrantedAuthority;
11import org.springframework.security.core.authority.SimpleGrantedAuthority;
12import org.springframework.security.core.userdetails.UserDetails;
13import org.springframework.security.core.userdetails.User;
14import org.springframework.stereotype.Component;
15
16@Component
17public class CustomAuthenticationProvider implements AuthenticationProvider {
18
19 boolean shouldAuthenticateAgainstThirdPartySystem = true;
20
21 @Override
22 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
23 String name = authentication.getName();
24 String password = authentication.getCredentials().toString();
25
26 if (name.equals("admin") && password.equals("password")) {
27 final List<GrantedAuthority> grantedAuths = new ArrayList<>();
28 grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
29 final UserDetails principal = new User(name, password, grantedAuths);
30 final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
31 return auth;
32 } else {
33 return null;
34 }
35
36 }
37
38 @Override
39 public boolean supports(Class<?> authentication) {
40
41 return authentication.equals(UsernamePasswordAuthenticationToken.class);
42 }
43
44}
45
46
1package com.frugalis;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.context.annotation.ComponentScan;
5import org.springframework.context.annotation.Configuration;
6import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10
11@Configuration
12@EnableWebSecurity
13@ComponentScan("com.frugalis")
14public class CustAuthProviderConfig extends WebSecurityConfigurerAdapter {
15
16 @Autowired
17 private CustomAuthenticationProvider authProvider;
18
19 @Override
20 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
21 auth.authenticationProvider(authProvider);
22 }
23
24 @Override
25 protected void configure(HttpSecurity http) throws Exception {
26 http.authorizeRequests().anyRequest().authenticated()
27 .and().httpBasic();
28 }
29}
30
31
32