当前位置: 首页 > wzjs >正文

楚雄做网站seo的中文是什么

楚雄做网站,seo的中文是什么,wordpress 密码看贴,o2o是什么意思的DAY30.1 Java核心基础 Spring Boot 整合安全框架 Spring Security 、Shiro Spring Security Spring Security 的核心功能包括认证、授权、攻击防护,通过大量的过滤器和拦截器进行请求的拦截和验证,实现安全校验的功能。 Spring Security 将校验逻辑…

DAY30.1 Java核心基础

Spring Boot 整合安全框架

Spring Security 、Shiro

Spring Security

Spring Security 的核心功能包括认证、授权、攻击防护,通过大量的过滤器和拦截器进行请求的拦截和验证,实现安全校验的功能。

Spring Security 将校验逻辑分发到不同的拦截器中,一个拦截器负责一种验证,比如UsernamePassword拦截器就是专门用来验证用户名和密码是否正确的,它会拦截登入请求,检查是否包含了用户名和密码,同时进行验证,如果没有则放行进入下一个拦截器

1、pom中添加依赖

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

2、创建一个index页面

<!DOCTYPE html>
<!--导入thymeleaf配置-->
<html lang="en" xmlns:th="http://www.thymeleaf.org"></html>
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>hello</h1></body>
</html>

3、访问页面(如果没登入认证)会跳转到 Spring Security 的登入验证界面

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

这个页面是Spring Security自带的页面,导入了依赖包就有了

然后用户名密码默认为user,密码为启动的时候生成的密码image-20250529223308283

然后他在拦截器UsernamePasswordAuthenticationFilter进行拦截验证的

4、配置Spring Security的账号密码

spring:security:user:name: adminpassword: 123456

除了登入的时候,Security还可以用于权限验证,比如admin账号可以访问所有页面,user角色账号只能访问某些界面,怎么实现呢?

创建一个配置类SecurityConfig
package org.nanfengspringboot01.config;import org.springframework.context.annotation.Configuration;
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;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {/*** 定义角色* @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new PasswordEncoderImpl()).withUser("user").password(new PasswordEncoderImpl().encode("123")).roles("USER").and().withUser("admin").password(new PasswordEncoderImpl().encode("123")).roles("USER","ADMIN");}/*** 定义权限* @param http* @throws Exception*/@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN").antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER')").anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll().and().csrf().disable();}
}
还需要一个实现类PasswordEncoderImpl
public class PasswordEncoderImpl implements PasswordEncoder {@Overridepublic String encode(CharSequence rawPassword) {return rawPassword.toString();}@Overridepublic boolean matches(CharSequence rawPassword, String encodedPassword) {return encodedPassword.equals(rawPassword.toString());}
}

解释一下配置代码:

http.authorizeRequests()

开始配置 URL 的访问权限控制。

.antMatchers("/admin").hasRole("ADMIN")

访问 /admin 页面必须具备 ADMIN 角色。

.antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER')")

访问 /index 页面,要求用户拥有 ADMINUSER 角色中的任意一个。

.anyRequest().authenticated()

除了 /admin/index 外,其他所有请求都必须登录后才能访问。

.and()
.formLogin()
.loginPage("/login")
.permitAll()

配置表单登录:

  • 登录页面为 /login(你需要自己定义一个该路径的登录页面)
  • 所有人都可以访问该登录页面(不需要登录)
.and()
.logout()
.permitAll()

配置登出功能:

  • 所有人都可以访问登出接口
.and()
.csrf()
.disable();

禁用 CSRF(跨站请求伪造)防护机制。

✅ 适合前后端分离、或使用 JWT 登录验证的项目。否则生产环境最好不要关闭 CSRF 防护。

控制器
@Controller
public class IndexController {@GetMapping("/index")private String index(){return "index";}@GetMapping("/admin")private String admin(){return "admin";}@GetMapping("/login")private String login(){return "login";}
}

访问页面index.html

会自动跳转到login.html登入界面

image-20250530160030670

但是user账户登入之后访问不了admin界面,因为user没有访问admin界面的权限

image-20250530160108229

连接数据库获取账号信息

创建数据库表Account

create table account(id int PRIMARY key auto_increment,username VARCHAR(11),password VARCHAR(11),role VARCHAR(11)
)

配置用户信息,注意role的写法需要写出ROLE_USER这样的格式,这个是Security的解析格式

image-20250530165855285

创建实体类

@Data
@Entity
public class Account  {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;@Columnprivate String username;@Columnprivate String password;@Columnprivate String role;
}

创建 AccountRepository,实现登录方法

@Repository
public interface AccountResitory extends JpaRepository<Account,Integer> {public Account findByUsername(String username);
}

重写UserDetailsService接口的loadUserByUsername方法,从数据库获取密码 ,返回一个User对象

@Service
public class UserDetailServiceImpl implements UserDetailsService {@Autowiredprivate AccountResitory accountResitory;/*** 通过重新这个方法来改变验证匹配密码的逻辑* @param username* @return* @throws UsernameNotFoundException*/@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {Account account = accountResitory.findByUsername(username);if(account==null){throw new UsernameNotFoundException("用户不存在");}else {String password = account.getPassword();Collection<GrantedAuthority> authorities = new ArrayList<>();SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(account.getRole());authorities.add(simpleGrantedAuthority);return new User(username,password,authorities);}}
}

修改Security的配置类,将这个UserDetailsService注入

auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder());

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService userDetailService;/*** 定义用户角色* @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(new PasswordEncoderImpl())
//                .withUser("user").password(new PasswordEncoderImpl().encode("123")).roles("USER")
//                .and()
//                .withUser("admin").password(new PasswordEncoderImpl().encode("123")).roles("USER","ADMIN");auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder());}@Beanpublic PasswordEncoder passwordEncoder() {return NoOpPasswordEncoder.getInstance();}/*** 定义权限* @param http* @throws Exception*/@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN").antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER')").anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll().and().csrf().disable();}
}
http://www.dtcms.com/wzjs/485942.html

相关文章:

  • 郑州做网站齿轮合肥关键词排名工具
  • 品牌网站制作报价网站建设公司企业网站
  • 什么是建设网站工具外国黄冈网站推广平台
  • 河北网站开发公司怎么做网络营销
  • 临安做网站58黄页网推广公司
  • 36氪网站用什么程序做的网页开发培训网
  • 自己可以做公司网站吗营销策略
  • 无锡做网站建设爱站网关键词搜索
  • 百度云网站建设教程百度识图扫一扫
  • 专做化妆品的网站15个常见关键词
  • 五个网站页面运营主要做什么工作
  • 武汉光谷做网站哪家好太原百度网站快速排名
  • 网站404页面怎么做网络推广员工作内容
  • 商业计划书短视频排名seo
  • 做培训网站哪家好百度资源分享网
  • 化妆品网站建设实施方案最近新闻大事
  • 1688网站建设厦门seo收费
  • 湖南工商网址扬州seo博客
  • 网站建设口号合肥seo外包平台
  • dw怎么做jsp网站google搜索引擎
  • 使用flask做前后端分离的网站百度站点
  • 个人博客网站取名快速网络推广
  • 南宁公司网站建设十大嵌入式培训机构
  • 债权债务交易网站开发鸡西网站seo
  • 泰州企业自助建站系统广州百度推广客服电话
  • 淮安营销型网站建设长沙营销型网站建设
  • 怎样优化网站西安网站推广慧创科技
  • 公司网站工程案例怎么做合肥网站优化技术
  • 网站的登录界面是怎么做的品牌营销策划方案怎么做才好
  • 成都家具网站建设搜外seo视频 网络营销免费视频课程