admin
2020-11-09 18f9c27e17a4bef2f1d6571bf113b9bb741046af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ks.consumerjdgiftcoupon.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
 
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    class CustomUserService implements UserDetailsService {
 
        @Override
        public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
            //通过名称查询用户
 
            return null;
        }
    }
 
    class CustomAuth implements AuthenticationProvider {
 
        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            authentication.setAuthenticated(true);
            return authentication;
        }
 
        @Override
        public boolean supports(Class<?> aClass) {
            return true;
        }
    }
 
 
    @Bean
    UserDetailsService customUserService() {
        return new CustomUserService();
    }
 
    @Bean
    AuthenticationProvider customAuth() {
        return new CustomAuth();
    }
 
    /**
     * 定义用户认证规则
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService());
        auth.authenticationProvider(customAuth());
    }
 
    /**
     * 定义授权规则
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
//                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }
 
 
}