基于内存方式 - 只作学习参考 1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2.login.html、index.html、fail.html login.html: <form method="post" action="/doLogin"> 用户名:<input id="username" name="username" type="text" /> 密码:<input id="password" name="password" type="password"/> <input type="submit" value="登录"/> </form> index.html: <h3><font color="#228b22">恭喜您,登录成功!</font></h3> fail.html: <h3><font color="#dc143c">很抱歉,登录失败!</font></h3> 3.LoginController @Controller public class LoginController { @RequestMapping("/login") public String loginPage() { return "login"; } @RequestMapping("/main") public String main() { return "index"; } @RequestMapping("/fail") public String fail() { return "fail"; } /** * 处理用户名和密码 * @param auth * @throws Exception */ @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder()) .withUser("admin") .password(new BCryptPasswordEncoder().encode("123456")) .roles("USER"); } } 4.SecurityConfig @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 配置对 HTTP 请求的安全拦截处理 @Override protected void configure(HttpSecurity http) throws Exception { // antMatchers("/static/**").permitAll() - "/static/**" 路径下的资源不需要认证拦截,全部放行 http.authorizeRequests().antMatchers("/static/**").permitAll() .anyRequest().authenticated() .and().formLogin() .and().csrf().disable() .formLogin().loginPage("/login").loginProcessingUrl("/doLogin") .defaultSuccessUrl("/main") .failureUrl("/fail") .permitAll(); } } 5.application.properties server.port=8086 # spring.application.name=perms-sample spring.main.allow-bean-definition-overriding=true spring.mvc.static-path-pattern=/static/** # thymeleaf 配置 spring.thymeleaf.enabled=true spring.thymeleaf.cache=false spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html # 数据库配置 spring.datasource.name=defaultDataSource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_plain?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=root123 # 连接池配置 spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.maximum-pool-size=8 spring.datasource.hikari.minimum-idle=4 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.max-lifetime=50000 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.pool-name=HikariCP # mybatis 配置 mybatis.mapper-locations=classpath:mappers/*xml mybatis.type-aliases-package=com.sky.biz.entity mybatis.configuration.map-underscore-to-camel-case=true 6.启动类 @Slf4j @SpringBootApplication(scanBasePackages = {"com.xxx"}) @MapperScan(basePackages = {"com.xxx.dao"}) public class SecuritySampleApplication { public static void main(String[] args) { ConfigurableApplicationContext application = SpringApplication.run(SecuritySampleApplication.class, args); } }
文章来源地址https://www.toymoban.com/news/detail-698861.html
文章来源:https://www.toymoban.com/news/detail-698861.html
到了这里,关于Spring Security - 基于内存快速demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!