整合Shiro

This commit is contained in:
2023-07-09 23:28:42 +08:00
parent b8a7fe4f53
commit 60b061b7c6
9 changed files with 156 additions and 0 deletions

View File

@@ -41,6 +41,13 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.11.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -0,0 +1,23 @@
package com.wuyiqi.netstateproc.config;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.Realm;
public class NetStateRealm implements Realm {
@Override
public String getName() {
return null;
}
@Override
public boolean supports(AuthenticationToken token) {
return false;
}
@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return null;
}
}

View File

@@ -0,0 +1,22 @@
package com.wuyiqi.netstateproc.config;
import org.apache.catalina.Realm;
import org.apache.shiro.spring.config.ShiroAnnotationProcessorConfiguration;
import org.apache.shiro.spring.config.ShiroBeanConfiguration;
import org.apache.shiro.spring.config.ShiroConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ShiroBeanConfiguration.class,
ShiroConfiguration.class,
ShiroAnnotationProcessorConfiguration.class})
public class ShiroConfig {
@Bean
public Realm realm() {
return null;
}
}

View File

@@ -0,0 +1,28 @@
package com.wuyiqi.netstateproc.controller;
import com.wuyiqi.netstateproc.model.eneity.User;
import com.wuyiqi.netstateproc.model.rest.req.LoginReq;
import com.wuyiqi.netstateproc.model.rest.resp.Resp;
import com.wuyiqi.netstateproc.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
public class UserController {
private final UserService userService;
@PostMapping("/login")
public Resp<String> login(@RequestBody LoginReq req) {
return null;
}
@GetMapping("/test")
public Resp<User> test() {
return Resp.success(userService.findByUsername("admin"));
}
}

View File

@@ -0,0 +1,18 @@
package com.wuyiqi.netstateproc.model.eneity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String password;
private String username;
}

View File

@@ -0,0 +1,9 @@
package com.wuyiqi.netstateproc.model.rest.req;
import lombok.Data;
@Data
public class LoginReq {
private String username;
private String password;
}

View File

@@ -0,0 +1,20 @@
package com.wuyiqi.netstateproc.model.rest.resp;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
@Data
@Accessors(chain = true)
@AllArgsConstructor
public class Resp<T> {
private Integer code;
private String message;
private T data;
public static <T> Resp<T> success(T data) {
return new Resp<>(HttpStatus.OK.value(), "success", data);
}
}

View File

@@ -0,0 +1,10 @@
package com.wuyiqi.netstateproc.respository;
import com.wuyiqi.netstateproc.model.eneity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
//@Repository
public interface UserRepo extends JpaRepository<User, Long> {
User findByUsername(String username);
}

View File

@@ -0,0 +1,19 @@
package com.wuyiqi.netstateproc.service;
import com.wuyiqi.netstateproc.model.eneity.User;
import com.wuyiqi.netstateproc.respository.UserRepo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class UserService {
private final UserRepo userRepo;
public User findByUsername(String username) {
return userRepo.findByUsername(username);
}
}