Shiro、Spring Security整合

Shiro、Spring Security

Spring Boot 整合 Spring Security

快速上? Spring Security

1、創(chuàng)建 Maven ?程,pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>2.1.5.RELEASE</version>
</parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> 

2、創(chuàng)建 Handler

package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloHandler {
@GetMapping("/index")
public String index(){
return "index";
 }
}

3、創(chuàng)建 HTML

<!DOCTYPE html>
<html lang="en"> <head><meta charset="UTF-8"> <title>Title</title>
</head> <body><h1>Hello World</h1>
</body>
</html> 4、創(chuàng)建 application.yml
spring:
 thymeleaf:
 prefix: classpath:/templates/
 suffix: .html

5、創(chuàng)建啟動類 Application

package com.southwind.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
 }
}

6、設(shè)置?定義密碼

spring:
 thymeleaf:
 prefix: classpath:/templates/
 suffix: .html
 security:
 user:
 name: admin
 password: 123123

權(quán)限管理
定義兩個 HTML 資源:index.html、admin.html,同時定義兩個?? ADMIN 和 USER,ADMIN 擁有
訪問 index.html 和 admin.html 的權(quán)限,USER 只有訪問 index.html 的權(quán)限。

7、創(chuàng)建 SecurityConfig 類。

package com.southwind.config;
import org.springframework.context.annotation.Configuration;
import
org.springframework.security.config.annotation.authentication.builders.Authent
icationManagerBuilder;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecu
rity;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityCo
nfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws
Exception {
auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
 .withUser("user").password(new
MyPasswordEncoder().encode("000")).roles("USER")
 .and()
 .withUser("admin").password(new
MyPasswordEncoder().encode("123")).roles("ADMIN","USER");
 }
@Override
protected 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();
 }
}

8、修改 Handler

package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloHandler {
@GetMapping("/index")
public String index(){
return "index";
 }
@GetMapping("/admin")
public String admin(){
return "admin";
 }
@GetMapping("/login")
public String login(){
return "login";
 }
} 

9、login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head><meta charset="UTF-8"> <title>Title</title>
</head> <body><form th:action="@{/login}" method="post">
?戶名:<input type="text" name="username"/><br/>
密碼:<input type="text" name="password"/><br/>
<input type="submit" value="登錄"/>
</form>
</body>
</html>
10、index.html
<!DOCTYPE html>
<html lang="en"> <head><meta charset="UTF-8"> <title>Title</title>
</head> <body><h1>Hello World</h1> <form action="/logout" method="post"> <input type="submit" value="退出"/>
</form>
</body>
</html>
11、admin.html
<!DOCTYPE html>
<html lang="en"> <head><meta charset="UTF-8"> <title>Title</title>
</head> <body><h1>后臺管理系統(tǒng)</h1> <form action="/logout" method="post"> <input type="submit" value="退出"/>
</form>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容