springsecurity使用

一项目添加springsecurity,保护方法不被调用,登录后可调用实现

1 依赖

c 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2 springsecurity配置

c 复制代码
//开启方法级保护,如:@PreAuthorize("has Authority('ROLE_ADMIN')")
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
    }
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("admin").password(new BCryptPasswordEncoder().encode("123")).roles("USER").build());
        return manager;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests()
                .antMatchers("/css/**","/index").permitAll()
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/a").anonymous()
                .and()
                .formLogin().loginPage("/login").failureUrl("/login-error")
                .and()
                .exceptionHandling().accessDeniedPage("/401");
        http.logout().logoutSuccessUrl("/");
    }
}

3 登录跳转被保护页面流程

首先进入免登录的首页,当点击链接进入受到保护页面时,此时因没有登录,页面自动跳到登录页面,在登录页面输入有USER角色的账号时,自动跳到请求的href="/user/index"后台

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
  <p>这个页面没有受到保护</p>
  <p th:text="${tip}"></p>
  <form action="#" th:action="@{/logout}" method="post">
    <input type="submit" value="登出">
  </form>
<ul>
    <li>点击<a href="/user/index" th:href="@{/user/index}">跳转到user/index已被保护的界面</a></li>
</ul>
</body>
</html>
相关推荐
空太Jun18 天前
Spring Security 角色权限&资源权限配置 学习笔记
笔记·学习·spring·mybatis·security·springsecurity
九皇叔叔22 天前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity
九皇叔叔23 天前
004-SpringSecurity-Demo 拆分环境
java·springboot3·springsecurity
九皇叔叔23 天前
006-SpringSecurity-Demo 跨域(CORS)配置
java·springboot3·springsecurity·跨域·cors
九皇叔叔23 天前
005-SpringSecurity-Demo 配置外部文件映射
java·springboot·文件·springsecurity
九皇叔叔24 天前
001-SpringSecurity-Demo 创建项目
java·springboot·springsecurity
创梦流浪人1 个月前
soli-admin一款开箱即用的RBAC后台项目
java·spring boot·vue3·springsecurity
浑水摸鱼仙君1 个月前
SpringSecurity和Flux同时使用报未认证问题
java·ai·flux·springsecurity·springai
知识即是力量ol3 个月前
一次完整的 Spring Security JWT 鉴权链路解析
java·后端·spring·鉴权·springsecurity
佛祖让我来巡山5 个月前
小明网站双登录系统实现——微信授权登录+用户名密码登录完整指南
oauth2·springsecurity·微信授权登录