SpringSecurity-3

文章目录

SpringSecurity整合SpringBoot集中式版

1.技术选型

SpringBoot2.1.3,SpringSecurity,MySQL,mybatis,jsp

2.初步整合认证第一版

1.创建工程并导入jar包

先只导入SpringBoot

<parent> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId>spring-boot-starter-parent</artifactId> 
	<version>2.1.3.RELEASE</version> 
	<relativePath/> 
</parent> 
<dependencies> 
	<dependency>
		<groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId> 
	</dependency> 
</dependencies>

2.提供处理器

@Controller 
@RequestMapping("/product") 
public class ProductController {
    
	@RequestMapping 
	@ResponseBody 
	public String hello(){
    
		return "success"; 
	} 
}

3.编写启动类

@SpringBootApplication 
public class SecurityApplication {
    
	public static void main(String[] args) {
    
		SpringApplication.run(SecurityApplication.class, args); 
	} 
}

4.测试效果

使用SpringBoot内置tomcat启动项目,即可访问处理器。
在这里插入图片描述

5.加入SpringSecurity的jar包

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

6.重启再次测试

SpringBoot已经为SpringSecurity提供了默认配置,默认所有资源都必须认证通过才能访问。
在这里插入图片描述
那么问题来了!此刻并没有连接数据库,也并未在内存中指定认证用户,如何认证呢?
其实SpringBoot已经提供了默认用户名user,密码在项目启动时随机生成,如图:
在这里插入图片描述
认证通过后可以继续访问处理器资源:
在这里插入图片描述

3.整合认证第二版【加入jsp使用自定义认证页面】

1.说明

SpringBoot官方是不推荐在SpringBoot中使用jsp的,那么到底可以使用吗?答案是肯定的!
不过需要导入tomcat插件启动项目,不能再用SpringBoot默认tomcat了。

2.导入SpringBoot的tomcat启动插件jar包

<dependency> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId>spring-boot-starter-tomcat</artifactId> 
</dependency> 
<dependency> 
	<groupId>org.apache.tomcat.embed</groupId> 
	<artifactId>tomcat-embed-jasper</artifactId> 
</dependency>

3.加入jsp页面等静态资源

在src/main目录下创建webapp目录
在这里插入图片描述
这时webapp目录并不能正常使用,因为只有web工程才有webapp目录,在pom文件中修改项目为web工程。
在这里插入图片描述
这时webapp目录,可以正常使用了!
在这里插入图片描述
导入第一天案例中静态资源,注意WEB-INF就不用了哈!
在这里插入图片描述
修改login.jsp中认证的url地址。
在这里插入图片描述
修改header.jsp中退出登录的url地址。
在这里插入图片描述

4.提供SpringSecurity配置类

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
	/*** 这里先不连接数据库了 */
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
		auth.inMemoryAuthentication() .withUser("user") 
									  .password("{noop}123") 
									  .roles("USER"); 
	}
	
	protected void configure(HttpSecurity http) throws Exception {
   
        http.authorizeRequests().antMatchers("/login.jsp", "/failer.jsp", "/css/**", "/img/**", "/plugins/**")
                                .permitAll()
                                .antMatchers("/**")
                                .hasAnyRole("USER")
                                .anyRequest()
                                .authenticated()
                                .and().formLogin()
                                .loginPage("/login.jsp")
                                .loginProcessingUrl("/login")
                                .successForwardUrl("/index.jsp")
                                .failureForwardUrl("/failer.jsp")
                                .permitAll()
                                .and()
                                .logout()
                                .logoutUrl("/logout")
                                .invalidateHttpSession(true)
                                .logoutSuccessUrl("/login.jsp")
                                .permitAll()
                                .and()
                                .csrf()
                                .disable();
    }
}

5.修改产品处理器

有页面了,就跳转一个真的吧!

@Controller 
@RequestMapping("/product") 
public class ProductController {
    
	@RequestMapping("/findAll") 
	public String findAll(){
    
		return "product-list"; 
	} 
}

6.配置视图解析器

在这里插入图片描述

7.使用tomcat插件启动项目

在这里插入图片描述

8.测试效果

自定义的认证页面
在这里插入图片描述
认证成功页面
在这里插入图片描述

4.整合认证第三版【数据库认证】

1.数据库环境准备

依然使用security_authority数据库,sql语句在第一天资料里。

2.导入数据库操作相关jar包

<dependency> 
	<groupId>mysql</groupId> 
	<artifactId>mysql-connector-java</artifactId> 
	<version>5.1.47</version> 
</dependency> 
<dependency> 
	<groupId>tk.mybatis</groupId> 
	<artifactId>mapper-spring-boot-starter</artifactId> 
	<version>2.1.5</version> 
</dependency>

3.在配置文件中添加数据库操作相关配置

在这里插入图片描述

4.在启动类上添加扫描dao接口包注解

在这里插入图片描述

5.创建角色pojo对象

这里直接使用SpringSecurity的角色规范

public class SysRole implements GrantedAuthority{
   
    private Integer id;
    private String roleName;
    private String roleDesc;

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public String getRoleName() {
   
        return roleName;
    }

    public void setRoleName(String roleName) {
   
        this.roleName = roleName;
    }

    public String getRoleDesc() {
   
        return roleDesc;
    }

    public void setRoleDesc(String roleDesc) {
   
        this.roleDesc = roleDesc;
    }

    //标记此属性不做json处理 
    @JsonIgnore
    @Override
    public String getAuthority() {
   
        return roleName;
    }
}

6.创建用户pojo对象

这里直接实现SpringSecurity的用户对象接口,并添加角色集合私有属性。
注意接口属性都要标记不参与json的处理。

public class SysUser implements UserDetails{
   
    private Integer id;
    private String username;
    private String password;
    private Integer status;
    private List<SysRole> roles = new ArrayList<>();

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public void setPassword(String password) {
   
        this.password = password;
    }

    public Integer getStatus() {
   
        return status;
    }

    public void setStatus(Integer status) {
   
        this.status = status;
    }

    public List<SysRole> getRoles() {
   
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
   
        this.roles = roles;
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
   
        return roles;
    }

    @Override
    public String getPassword() {
   
        return password;
    }

    @Override
    public String getUsername() {
   
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
   
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
   
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
   
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值