1.Thư Nguyên
1 2 3 4 5 | <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> |
2. @EnableAuthorizationServer
Máy chủ ủy quyền
Cơ sở dữ liệu của bạn, mã hóa và mã hóa bộ nhớ của bạn
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 | @Configuration @EnableAuthorizationServer public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private TokenStore tokenStore; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT", "USER") .scopes("read", "write") .autoApprove(true) .secret(passwordEncoder().encode("secret")); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints .authenticationManager(authenticationManager) .tokenStore(tokenStore); } @Bean public TokenStore tokenStore() { return new InMemoryTokenStore(); } } |
Quan ta xem qua thiết bị:
tokenStore
ở đây là một ứng dụng mã thông báo InMemoryTokenStore
nghĩa là mã thông báo mã thông báo bộ nhớ của chúng tôi có thể sử dụng cơ sở dữ liệu JdbcTokenStore
cơ sở dữ liệu mã thông báo.
withClient ("client")
là user người dùng trong ngân hàng. trong đó tổng hợp trong trò chơi ta ta là khách hàng
uthorizedGrantTypes ("password", "authorization_code", "refresh_token", "implicit")
dịch vụ mật khẩu
secret (passwordEncoder (). encode ("password"))
là mật khẩu của "client"
3. Cấu hình bảo mật
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 | @Configuration @EnableWebSecurity public class AuthServerWebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private PasswordEncoder passwordEncoder; @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(final HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated(); } @Bean @Override public UserDetailsService userDetailsService() { UserDetails user = User.builder().username("user").password(passwordEncoder.encode("password")). roles("USER").build(); UserDetails userAdmin = User.builder().username("admin").password(passwordEncoder.encode("password")). roles("ADMIN").build(); return new InMemoryUserDetailsManager(user, userAdmin); } } |
userDetailsService
bộ người dùng @Override userDetailsService
và thiết lập hai người dùng và trong bộ nhớ trong khi thanh toán khi bạn @Override userDetailsService
bạn truy vấn cơ sở dữ liệu
4. Trình điều khiển
1 2 3 4 5 6 7 8 9 10 | @RestController @RequestMapping @EnableResourceServer public class TestController { @GetMapping("/test") public String test() { return "oke"; } } |
5. bảo mật tra
Phần cứng của chúng tôi
Kết quả của mục này là tính năng của chúng tôi khi bạn đăng nhập
Bạn có thể đăng nhập vào mạng token với đường dẫn /oauth/token
1 2 3 4 5 6 7 8 | curl -X POST http://localhost:8686/oauth/token -H 'Authorization: Basic bm9uYW1lOjEyMzQ1Ng==' -H 'Cache-Control: no-cache' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Postman-Token: 7ec2be4e-a7e6-4049-ad5e-c96829683cb1' -d 'grant_type=password&username=user&password=password' |
Quảng cáo ta sao chép access_token và quay lại api /test
mẹo token vào Tiêu đề Authorization
Như là một phần của bạn
6.K lòng
Phần còn lại của phần mềm trong phần mềm của bạn