如何在自己网站做解析apiwordpress 火
- 作者: 五速梦信息网
- 时间: 2026年03月21日 09:49
当前位置: 首页 > news >正文
如何在自己网站做解析api,wordpress 火,天河门户网站建设,有口碑的宁波网站建设家庭物联网系统的代码和说明#xff0c;包括用户认证、设备控制、数据监控、通知和警报、日志记录以及WebSocket实时更新功能。
项目结构
plaintext home-iot-system ├── backend │ └── src │ └── main │ └── java │ └…家庭物联网系统的代码和说明包括用户认证、设备控制、数据监控、通知和警报、日志记录以及WebSocket实时更新功能。
项目结构
plaintext home-iot-system ├── backend │ └── src │ └── main │ └── java │ └── com │ └── example │ └── homeiot │ ├── config │ ├── controller │ ├── model │ ├── repository │ ├── service │ ├── websocket │ └── HomeIotApplication.java ├── frontend │ ├── public │ └── src │ ├── components │ ├── pages │ ├── services │ └── App.js ├── pom.xml └── package.json
后端Spring Boot
pom.xml
xml project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.example/groupId artifactIdhome-iot-system/artifactId version1.0-SNAPSHOT/version dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-jpa/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-security/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId /dependency dependency groupIdcom.h2database/groupId artifactIdh2/artifactId scoperuntime/scope /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-websocket/artifactId /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project
HomeIotApplication.java
java package com.example.homeiot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication public class HomeIotApplication { public static void main(String[] args) { SpringApplication.run(HomeIotApplication.class, args); } }
用户认证和角色管理
SecurityConfig.java
java package com.example.homeiot.config; import com.example.homeiot.service.UserService; import org.springframework.context.annotation.Bean; 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.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private final UserService userService; public SecurityConfig(UserService userService) { this.userService userService; } Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService).passwordEncoder(passwordEncoder()); } Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers(/api/).authenticated() .antMatchers(/admin/).hasRole(ADMIN) .anyRequest().permitAll() .and() .formLogin() .and() .httpBasic(); } Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
Role.java
java package com.example.homeiot.model; import javax.persistence.*; import java.util.Set; Entity public class Role { Id GeneratedValue(strategy GenerationType.AUTO) private Long id; private String name; ManyToMany(mappedBy roles) private SetUser users; // getters and setters }
User.java
java package com.example.homeiot.model; import javax.persistence.*; import java.util.Set; Entity public class User { Id GeneratedValue(strategy GenerationType.AUTO) private Long id; private String username; private String password; ManyToMany(fetch FetchType.EAGER) JoinTable( name user_role, joinColumns JoinColumn(name user_id), inverseJoinColumns JoinColumn(name role_id)) private SetRole roles; // getters and setters }
UserRepository.java
java package com.example.homeiot.repository; import com.example.homeiot.model.User; import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepositoryUser, Long { User findByUsername(String username); }
RoleRepository.java
java package com.example.homeiot.repository; import com.example.homeiot.model.Role; import org.springframework.data.repository.CrudRepository; public interface RoleRepository extends CrudRepositoryRole, Long { }
UserService.java
java package com.example.homeiot.service; import com.example.homeiot.model.User; import com.example.homeiot.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; Service public class UserService implements UserDetailsService { Autowired private UserRepository userRepository; Autowired private PasswordEncoder passwordEncoder; public User save(User user) { user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepository.save(user); } public User findByUsername(String username) { return userRepository.findByUsername(username); } Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user userRepository.findByUsername(username); if (user null) { throw new UsernameNotFoundException(User not found); } return org.springframework.security.core.userdetails.User .withUsername(username) .password(user.getPassword()) .authorities(user.getRoles().stream() .map(role - ROLE_ role.getName().toUpperCase()) .toArray(String[]::new)) .build(); } }
设备数据监控和日志记录
Device.java
java package com.example.homeiot.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.time.LocalDateTime; Entity public class Device { Id GeneratedValue(strategy GenerationType.AUTO) private Long id; private String name; private String status; private String data; private LocalDateTime lastUpdated; // getters and setters }
DeviceLog.java
java package com.example.homeiot.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.time.LocalDateTime; Entity public class DeviceLog { Id GeneratedValue(strategy GenerationType.AUTO) private Long id; private Long deviceId; private String status; private String data; private LocalDateTime timestamp; // getters and setters }
DeviceLogRepository.java
java package com.example.homeiot.repository; import com.example.homeiot.model.DeviceLog; import org.springframework.data.repository.CrudRepository; import java.util.List; public interface DeviceLogRepository extends CrudRepositoryDeviceLog, Long { ListDeviceLog findByDeviceId(Long deviceId); }
DeviceRepository.java
java package com.example.homeiot.repository; import com.example.homeiot.model.Device; import org.springframework.data.repository.CrudRepository; public interface DeviceRepository extends CrudRepositoryDevice, Long { }
DeviceService.java
java package com.example.homeiot.service; import com.example.homeiot.model.Device; import com.example.homeiot.model.DeviceLog; import com.example.homeiot.repository.DeviceLogRepository; import com.example.homeiot.repository.DeviceRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; Service public class DeviceService { Autowired private DeviceRepository deviceRepository; Autowired private DeviceLogRepository deviceLogRepository; Autowired private SimpMessagingTemplate messagingTemplate; public ListDevice getAllDevices() { return (ListDevice) deviceRepository.findAll(); } public Device addDevice(Device device) { device.setLastUpdated(LocalDateTime.now()); return deviceRepository.save(device); } public Device updateDeviceStatus(Long id, String status) { Device device deviceRepository.findById(id).orElseThrow(() - new RuntimeException(Device not found)); device.setStatus(status); device.setLastUpdated(LocalDateTime.now()); deviceRepository.save(device); DeviceLog log new DeviceLog(); log.setDeviceId(id); log.setStatus(status); log.setTimestamp(LocalDateTime.now()); deviceLogRepository.save(log); messagingTemplate.convertAndSend(/topic/devices, device); return device; } public ListDeviceLog getDeviceLogs(Long deviceId) { return deviceLogRepository.findByDeviceId(deviceId); } }
DeviceController.java
java package com.example.homeiot.controller; import com.example.homeiot.model.Device; import com.example.homeiot.model.DeviceLog; import com.example.homeiot.service.DeviceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.web.bind.annotation.*; import java.util.List; RestController RequestMapping(/api/devices) public class DeviceController { Autowired private DeviceService deviceService; GetMapping public ListDevice getAllDevices() { return deviceService.getAllDevices(); } PostMapping public Device addDevice(RequestBody Device device) { return deviceService.addDevice(device); } PutMapping(/{id}/status) public Device updateDeviceStatus(PathVariable Long id, RequestParam String status) { return deviceService.updateDeviceStatus(id, status); } GetMapping(/{id}/logs) public ListDeviceLog getDeviceLogs(PathVariable Long id) { return deviceService.getDeviceLogs(id); } MessageMapping(/changeStatus) SendTo(/topic/devices) public Device changeDeviceStatus(Device device) { return deviceService.updateDeviceStatus(device.getId(), device.getStatus()); } }
WebSocket 实时更新
WebSocketConfig.java
java package com.example.homeiot.websocket; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; Configuration EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker(/topic); config.setApplicationDestinationPrefixes(/app); } Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint(/ws).withSockJS(); } }
前端React
package.json
json { name: home-iot-frontend, version: 1.0.0, dependencies: { axios: ^0.21.1, react: ^17.0.2, react-dom: ^17.0.2, react-router-dom: ^5.2.0, react-scripts: 4.0.3, sockjs-client: ^1.5.0, stomp/stompjs: ^6.1.0 }, scripts: { start: react-scripts start, build: react-scripts build, test: react-scripts test, eject: react-scripts eject } }
App.js
jsx import React, { useState, useEffect } from react; import axios from axios; import SockJS from sockjs-client; import { Stomp } from stomp/stompjs;
function App() { const [devices, setDevices] useState([]); const [deviceName, setDeviceName] useState(); const [client, setClient] useState(null); useEffect(() { fetchDevices(); connectWebSocket(); }, []); const fetchDevices () { axios.get(/api/devices) .then(response setDevices(response.data)) .catch(error console.error(Error fetching devices:, error)); }; const addDevice () { axios.post(/api/devices, { name: deviceName, status: off }) .then(response { setDevices([…devices, response.data]); setDeviceName(); }) .catch(error console.error(Error adding device:, error)); }; const updateDeviceStatus (deviceId, status) { axios.put(/api/devices/${deviceId}/status, null, { params: { status } }) .then(response { const updatedDevices devices.map(device device.id deviceId ? response.data : device); setDevices(updatedDevices); }) .catch(error console.error(Error updating device status:, error)); }; const connectWebSocket () { const socket new SockJS(/ws); const stompClient Stomp.over(socket); stompClient.connect({}, frame { console.log(Connected: frame); stompClient.subscribe(/topic/devices, message { const updatedDevice JSON.parse(message.body); setDevices(prevDevices prevDevices.map(device device.id updatedDevice.id ? updatedDevice : device) ); }); }); setClient(stompClient); }; return ( div h1Home IoT System/h1 input typetext value{deviceName} onChange{e setDeviceName(e.target.value)} placeholderEnter device name / button onClick{addDevice}Add Device/button ul {devices.map(device ( li key{device.id} {device.name} - {device.status} button onClick{() updateDeviceStatus(device.id, device.status off ? on : off)} Toggle Status /button /li ))} /ul /div ); }
export default App;
系统具备了用户认证、角色管理、设备数据监控、日志记录、通知和警报、以及WebSocket实时更新功能。
相关文章
-
如何在中国建设银行网站转账wordpress修改后天地址
如何在中国建设银行网站转账wordpress修改后天地址
- 技术栈
- 2026年03月21日
-
如何在一个地方建设网站深圳建网站公司
如何在一个地方建设网站深圳建网站公司
- 技术栈
- 2026年03月21日
-
如何在网站做投票wordpress 阅读
如何在网站做投票wordpress 阅读
- 技术栈
- 2026年03月21日
-
如何增加网站的访问量制作网站是什么专业
如何增加网站的访问量制作网站是什么专业
- 技术栈
- 2026年03月21日
-
如何增加网站反链做网站 前端
如何增加网站反链做网站 前端
- 技术栈
- 2026年03月21日
-
如何增加网站访问量seo秘籍优化课程
如何增加网站访问量seo秘籍优化课程
- 技术栈
- 2026年03月21日






