zoukankan      html  css  js  c++  java
  • spring boot , spring security 安全的认证

    pom 文件

    -------------------------------------------------------------------

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    application 文件
    ----------------------------------------------------------------------
    @SpringBootApplication
    public class Security01Application {

    public static void main(String[] args) {
    SpringApplication.run(Security01Application.class, args);
    }
    }
    
    
    Service 层
    ----------------------------------------------------------------------
    @Service
    public class RegService {
    public int reg(String username, String password) {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
    String encodePasswod = encoder.encode(password);
    return saveToDb(username, encodePasswod);
    }

    private int saveToDb(String username, String encodePasswod) {
    return 0;
    }
    }

    config 层
    -------------------------------------------------------------------
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
    public class MultiHttpSecurityConfig{
    @Bean
    PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
    }
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
    .withUser("root")
    .password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq")
    .roles("ADMIN", "DBA")
    .and()
    .withUser("admin")
    .password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq")
    .roles("ADMIN", "USER")
    .and()
    .withUser("sang")
    .password("$2a$10$eUHbAOMq4bpxTvOVz33LIehLe3fu6NwqC9tdOcxJXEhyZ4simqXTC")
    .roles("USER");
    }
    @Configuration
    @Order(1)
    public static class AdminSecurityConfig
    extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/admin/**").authorizeRequests()
    .anyRequest().hasRole("ADMIN");
    }
    }
    @Configuration
    public static class OtherSecurityConfig
    extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .formLogin()
    .loginProcessingUrl("/login")
    .permitAll()
    .and()
    .csrf()
    .disable();
    }
    }
    }

    ------------------
    @Service
    public class MethodService {
    @Secured("ROLE_ADMIN")
    public String admin() {
    return "hello admin";
    }
    @PreAuthorize("hasRole('ADMIN') and hasRole('DBA')")
    public String dba() {
    return "hello dba";
    }
    @PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
    public String user() {
    return "user";
    }
    }

    controller 层
    ----------------------------------------------------------

    @RestController
    public class HelloController {
    @GetMapping("/admin/hello")
    public String admin() {
    return "hello admin!";
    }

    @GetMapping("/admin/db/hello")
    public String admin2() {
    return "/admin/db/hello";
    }

    @GetMapping("/user/hello")
    public String user() {
    return "hello user!";
    }

    @GetMapping("/db/hello")
    public String dba() {
    return "hello dba!";
    }

    @Autowired
    MethodService methodService;

    @GetMapping("/hello")
    public String hello() {
    String user = methodService.user();
    return user;
    }

    @GetMapping("/hello2")
    public String hello2() {
    String admin = methodService.admin();
    return admin;
    }

    @GetMapping("/hello3")
    public String hello3() {
    String dba = methodService.dba();
    return dba;
    }
    }




  • 相关阅读:
    redis在centos7下安装(源码编译)
    Centos7之Gcc安装
    Jmeter工具之上传图片,上传音频文件接口
    什么是系统平均负载(Load average)
    sonar+Jenkins 构建代码质量自动化分析平台
    数据库主从相关配置参数说明
    现有数据库配置主从同步
    MySQL5.7多主一从(多源复制)同步配置
    MySQL5.7主从从配置
    MySQL5.7主从同步配置
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14040895.html
Copyright © 2011-2022 走看看