Dragoon 3 de fev. de 2017
joao0212 3 de fev. de 2017
Web XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< web - app xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xsi : schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id = "WebApp_ID" version = "3.1" >
< display - name > RecuperarDados </ display - name >
< welcome - file - list >
< welcome - file > index . htm </ welcome - file >
< welcome - file > index . jsp </ welcome - file >
< welcome - file > default . html </ welcome - file >
< welcome - file > default . htm </ welcome - file >
< welcome - file > default . jsp </ welcome - file >
</ welcome - file - list >
< session - config >
< session - timeout > 1 </ session - timeout >
</ session - config >
< servlet >
< servlet - name > springmvc </ servlet - name >
< servlet - class >
org . springframework . web . servlet . DispatcherServlet
</ servlet - class >
< init - param >
< param - name > contextConfigLocation </ param - name >
< param - value >
/ WEB - INF / spring - context . xml
</ param - value >
</ init - param >
< load - on - startup > 1 </ load - on - startup >
</ servlet >
< servlet - mapping >
< servlet - name > springmvc </ servlet - name >
< url - pattern >/</ url - pattern >
</ servlet - mapping >
</ web - app >
Spring Context
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns : context = "http://www.springframework.org/schema/context"
xmlns : mvc = "http://www.springframework.org/schema/mvc"
xsi : schemaLocation = "http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
< context : component - scan base - package = "br.com.sicoob." />
< mvc : annotation - driven />
< mvc : interceptors >
< bean class =" br . com . sicoob . interceptor . AutorizaInterceptor " />
</mvc:interceptors>
<bean class=" org . springframework . web . servlet . view . InternalResourceViewResolver ">
<property name=" prefix " value=" / WEB - INF / views / "/>
<property name=" suffix " value=" . jsp "/>
</bean>
<bean id=" DataSource " class=" org . apache . commons . dbcp . BasicDataSource ">
<property name=" driverClassName " value=" com . microsoft . sqlserver . jdbc . SQLServerDriver " />
<property name=" url " value=" jdbc : sqlserver : //:1433;databaseName=" />
< property name = "username" value = "" />
< property name = "password" value = "" />
</ bean >
< mvc : resources mapping = "/resources/**" location = "/resources/" />
< mvc : annotation - driven />
</ beans >
Form
<% @ taglib prefix = "spring" uri = "http://www.springframework.org/tags" %>
<% @ page language = "java" contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1" %>
<! DOCTYPE html SYSTEM "about:legacy-compat" >
< html >
< head >
< spring : url value = "/resources/css/bootstrap.min.css" var = "myCss" ></ spring : url >
< spring : url value = "/resources/js/bootstrap.min.js" var = "myJs" ></ spring : url >
< script src = "https://code.jquery.com/jquery-3.1.1.slim.min.js" ></ script >
< link href = "${myCss }" rel = "stylesheet" />
< script src = "${myJs }" ></ script >
< title > Dados Telespazio </ title >
</ head >
< body >
< div class =" row ">
<div class=" col - md - 4 "></div>
<div class=" col - md - 4 ">
<div class=" panel panel - primary ">
<div class=" panel - heading " align=" center ">
<h3 class=" panel - title ">Faça seu login</h3>
</div>
<div class=" panel - body ">
<form action=" efetuaLogin " method=" post ">
<input name=" login " type=" text " class=" form - control " placeholder=" Login ">
<input name=" senha " type=" password " class=" form - control " placeholder=" Senha ">
<button type=" submit " class=" btn btn - default ">Acessar dados</button>
</form>
</div>
</div>
</div>
<div class=" col - md4 "></div>
</div>
</body>
</html>
DAO
package br.com.sicoob.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import br.com.sicoob.entidade.Usuario;
@Repository
public class UsuarioDao {
private final Connection con;
@Autowired
public UsuarioDao(DataSource ds){
try{
this.con = ds.getConnection();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public boolean existeUsuario(Usuario usuario) {
if(usuario == null){
throw new IllegalArgumentException(" Usuário não deve ser nulo ");
}
try{
PreparedStatement ppstt = this.con.prepareStatement(" select * from usuarios where login = ? and senha = ? ");
ppstt.setString(1, usuario.getLogin());
ppstt.setString(2, usuario.getSenha());
ResultSet rs = ppstt.executeQuery();
boolean encontrado = rs.next();
rs.close();
ppstt.close();
return encontrado;
}catch (SQLException e) {
throw new RuntimeException();
}
}
}
Controller
package br.com.sicoob.controllers;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import br.com.sicoob.dao.UsuarioDao;
import br.com.sicoob.entidade.Usuario;
@Controller
public class ControllerLogin {
public UsuarioDao dao;
@Autowired
public ControllerLogin(UsuarioDao dao) {
this.dao = dao;
}
@RequestMapping(" loginForm ")
public String loginForm(){
return " formulario - login ";
}
@RequestMapping(" efetuaLogin ")
public String efetuaLogin(Usuario usuario, HttpSession session){
if(this.dao.existeUsuario(usuario)){
session.setAttribute(" usuarioLogado ", usuario);
return " menu ";
}else{
return " redirect : loginForm ";
}
}
}
joao0212 3 de fev. de 2017
Como eu informei, a validação do DAO, eu queria trocar pelo AD aqui da empresa, porém passando os dados pelo form …
Alguém poderia dar umas dicas?
joao0212 6 de fev. de 2017
Fiz uma forma de autenticação , mas queria algo mais elegante . Algo poderia ajudar ?
@SuppressWarnings ( "unchecked" )
public boolean existeUsuario ( Usuario usuario ) {
@SuppressWarnings ( "rawtypes" )
Hashtable authEnv = new Hashtable ( 11 );
authEnv . put ( Context . INITIAL_CONTEXT_FACTORY , "com.sun.jndi.ldap.LdapCtxFactory" );
authEnv . put ( Context . PROVIDER_URL , "" );
authEnv . put ( Context . SECURITY_AUTHENTICATION , "simple" );
authEnv . put ( Context . SECURITY_PRINCIPAL , usuario . getLogin () + "@.br" );
authEnv . put ( Context . SECURITY_CREDENTIALS , usuario . getSenha ());
System . out . println ( authEnv . keys ());
try {
@SuppressWarnings ( "unused" )
DirContext authContext = new InitialDirContext ( authEnv );
} catch ( AuthenticationException authEx ){
throw new RuntimeException ();
} catch ( NamingException namEx ){
throw new RuntimeException ();
}
return true ;
}
}