Spring Boot créent une application Web et intègre le modèle de thymeleaf pour obtenir la connexion. Ce qui suit est la configuration de pom.xml
<? xml version = "1.0" Encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" XSI: ScheMalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modeVersion> 4.0.0 </ Modelversion> <ProupId> Examin </prounid> <ArtifActid> Examin <packaging> jar </packaging> <version> 1.0-snapshot </-version> <! - Configuration de base de Spring Boot -> <parent> <proupId> org.springframework.boot </proncId> <Artefactive> printemps-boot-starter -arent </ artifactid> <version> 1.2.7.release </Degease> </parent> <! Version -> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <start-class> com.study.app </ start-class> <java.version> 1.7 </java.version> <shiro.version> 1.3.0 </shiro.version> <fuild> <handins> <proupId> org.springframework.boot </prômId> <ArtifActid> printemps-boot-maven-Plugin </ artifactId> <Dendency> </Dendecces> </Glugin> </Glugins> </uild> <peedences> <Detences> <proupId> org.springFramework. <ArtefactId> Spring-boot-starter-web </ artifactid> </dependency> <! - Le package Jar de JPA, qui exploite la base de données, est similaire à HiberNate -> <dependency> <proupId> org.springframework.boot </proupId> <Artifactid> Spring-Boot-starter-data-jpa </ artifactID> <! - THYMELELAF TEMPLAT JAR -> <Dependency> <ProupId> org.springframework.boot </proncId> <ArtefactId> printemp-boot-starter-thymealeaf </retifactid> </dependency> <! - Mysql Driver -> <Detendency> <proupId> mysql </roulid> <ArtefactId> MySQL-Connector-Java </ Artifactid> </Dependency> <! - Ajouter Restfull Support -> <Dedency> <ProupId> javax.ws.rs </proupId> <Retefactive> javax.ws.rs-api </ artifactid> <version> 2.0.1 </Dection> </Dedency> </Dependency> <GroupId> net.bull.javamelody </proupId> <Artifactid> javamelody-core </retifactid> <version> 1.53.0 </ version> </dependency> <! - Ajouter le pool de connexions de données Druid -> <dependency> <proupId> com.alibaba </prouprid> <artifactive> druid </rtifid> </Dependency> <! - Ajouter l'authentification de l'autorisation -> <dependency> <proupId> org.apache.shiro </proupId> <Artifactid> shiro-core </ artifactive> <Desentency> <proupId> org.apache.shiro </prètement> <ArtefactId> Shiro-Spring </letefactId> <Dersion> $ {shiro.version} </ version> </ Dependency> <Dendency> <ProupId> org.apache.shiro </proupId> <ArtifActid> Shiro-web </ artifactid> <Désentement> $ {Shiro.Version} </Dero version> <Dedency> <Dedency> <GroupId> org.apache.shiro </proupId> <Artifactid> shiro-ehcache </artifactid> <version> $ {shiro.version} </-version> </dependency> <! - Intégration de Shiro et Shiro -> <dependency> <proupId> com.github.theborakompanioni </prouvend> <ArtefactId> Thymeleaf-Extras-Shiro </Retifactid> <DERNÉRATION> 1.2.1 </DERNIERSE> </DENDENCENCE> </DENDENDENCES> </ Project>Méthode d'entrée principale
import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.enabableAutoconfiguration; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.context.web.springotesvervleniter; org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; / ** * créé par 2016/12/8. * / @ Configuration @ componentscan @ perteAutoConfigurationPublic Class App étend SpringbootServletInitializer {public static void main (String [] args) {SpringApplication.Run (app.class, args); }}Soumettre le code de formulaire sur la page de connexion,
<form role = "form" th: action = "@ {/ user / ligin}" th: method = "post"> <input type = "text" placeholder = "username" requis = "requise" name = "username" /> <input type = "passway" placeholder = "passway" requis = "requis" name = "passwprd" /> <bouton type = "soumi"> login value = "Remember-me" /> Remember Me </ Label> </ Form>Code de contrôleur
package com.study.system.contller; import com.study.model.contller.basecontrller; import com.study.model.po.user; import com.study.system.services.uservices; import org.springframework.beans.factory.annotation.autowired; import; org.springframework.sterreotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestMethod; import org.springframework.web.bind.annotation.restController; Import Java. / ** **; Gestion des utilisateurs * créée par 2016/12/12. * / @ Contrôleur @ requestmapping (value = "/ user") public class userContrller étend BasEContrller {@RequestMapping (value = "/ login", méthode = requestMethod.Post) public String Login (utilisateur utilisateur) {try {if (userServices.hasUser (user)) {return "redirect: / user / index";; } else {return "redirect: /"; }} catch (exception e) {logger.error ("Login a échoué:" + e, e); } return "redirect: /"; } @RequestMapping (value = "/ index", méthode = requestMethod.get) public String index () {try {} catch (exception e) {logger.error ("Login a échoué:" + e, e); } return "page / index / index"; } @Autowired Private UserServices UserServices;}Où UserServices est une interface professionnelle. BaseContller résume la classe de base du contrôleur pour lui-même.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.