Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.01 KB | None | 0 0
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import ro.nordicparts.odin.frontend.managedbeans.Login;
  4.  
  5. import javax.faces.context.ExternalContext;
  6. import javax.faces.context.FacesContext;
  7. import javax.servlet.*;
  8. import javax.servlet.annotation.WebFilter;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12.  
  13. @WebFilter("/views/*")
  14. public class AuthenticationFilter implements Filter {
  15.  
  16. private static final Logger LOG = LoggerFactory.getLogger(AuthenticationFilter.class);
  17.  
  18. private FilterConfig config;
  19.  
  20. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
  21. LOG.info("AuthFilter doFilter");
  22.  
  23. HttpServletRequest req = (HttpServletRequest) request;
  24.  
  25. Login auth = (Login) req.getSession().getAttribute("login");
  26.  
  27. if (auth != null && auth.isLoggedIn()) {
  28. // User is logged in, so just continue request.
  29. chain.doFilter(request, response);
  30. } else {
  31. // User is not logged in, so redirect to index.
  32. HttpServletResponse res = (HttpServletResponse) response;
  33. // Also check ajax requests, these need a special redirect response
  34. if ("partial/ajax".equals(((HttpServletRequest) request).getHeader("Faces-Request"))) {
  35. res.setContentType("text/xml");
  36. res.getWriter()
  37. .append("<?xml version="1.0" encoding="UTF-8"?>")
  38. .printf("<partial-response><redirect url="%s"></redirect></partial-response>", req.getContextPath() + "/login.xhtml");
  39. } else {
  40. res.sendRedirect(req.getContextPath() + "/login.xhtml");
  41. }
  42. }
  43. }
  44.  
  45. public void init(FilterConfig config) throws ServletException {
  46. this.config = config;
  47. }
  48.  
  49. public void destroy() {
  50. config = null;
  51. }
  52. }
  53.  
  54. import org.slf4j.Logger;
  55. import org.slf4j.LoggerFactory;
  56. import ro.nordicparts.odin.model.jaxb.LoginResponse;
  57.  
  58. import javax.annotation.PostConstruct;
  59. import javax.faces.application.FacesMessage;
  60. import javax.faces.bean.ManagedBean;
  61. import javax.faces.bean.ManagedProperty;
  62. import javax.faces.bean.SessionScoped;
  63. import javax.faces.context.FacesContext;
  64. import java.io.Serializable;
  65. import java.util.Locale;
  66.  
  67.  
  68. @ManagedBean(name="login")
  69. @SessionScoped
  70. public class Login implements Serializable {
  71.  
  72. private static final Logger LOG = LoggerFactory.getLogger(Login.class);
  73.  
  74. public static final String AUTH_KEY = "app.user.name";
  75.  
  76. @ManagedProperty(value="#{sessionData}")
  77. private SessionData sessionData;
  78.  
  79. private String username;
  80. private String password;
  81.  
  82. private static final long serialVersionUID = 2156313213089107389L;
  83.  
  84. public Login() { }
  85.  
  86. public boolean isLoggedIn() {
  87. return sessionData.getLoginResponse() != null;
  88. }
  89.  
  90. public String logoutAction() {
  91. FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
  92. .remove(AUTH_KEY);
  93. sessionData.setLoginResponse(null);
  94. return "/login.xhtml";
  95. }
  96.  
  97. public String loginAction() {
  98. ILoginBO bo = (ILoginBO) ServiceFactory.getServiceImpl(LoginBO.class);
  99.  
  100. try {
  101. LoginResponse loginResponse = loginBO.loginAction(username, password);
  102. LoginResponse loginResponse = new LoginResponse();
  103. sessionData.setLoginResponse(loginResponse);
  104. } catch (NotAuthorizedException e) {
  105. FacesContext context = FacesContext.getCurrentInstance();
  106. String value = context.getApplication().evaluateExpressionGet(context, "#{msg['"+e.getMessage()+"']}", String.class);
  107. FacesMessage message = new FacesMessage(value);
  108. FacesContext.getCurrentInstance().addMessage(null, message);
  109. return "login.xhtml";
  110. }
  111. return "views/index.xhtml";
  112. }
  113.  
  114. public String registerAction() {
  115. return "register.xhtml";
  116. }
  117.  
  118. public SessionData getSessionData() {
  119. return sessionData;
  120. }
  121.  
  122. public void setSessionData(SessionData sessionData) {
  123. this.sessionData = sessionData;
  124. }
  125.  
  126. public String getUsername() {
  127. return username;
  128. }
  129.  
  130. public void setUsername(String username) {
  131. this.username = username;
  132. }
  133.  
  134. public String getPassword() {
  135. return password;
  136. }
  137.  
  138. public void setPassword(String password) {
  139. this.password = password;
  140. }
  141. }
  142.  
  143. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  144. <html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  145. xmlns:f="http://java.sun.com/jsf/core"
  146. xmlns:ui="http://java.sun.com/jsf/facelets"
  147. xmlns:h="http://java.sun.com/jsf/html"
  148. xmlns:a4j="http://richfaces.org/a4j"
  149. xmlns:rich="http://richfaces.org/rich">
  150.  
  151.  
  152. <h:head>
  153. <title><h:outputText value="#{msg['login.title']}" /></title>
  154. <link rel="stylesheet" type="text/css"bhref="#{request.contextPath}/style/style2.css" />
  155. </h:head>
  156.  
  157. <h:body>
  158. <div id="container">
  159. <h:form class="login">
  160. <fieldset>
  161. <LEGEND>Login</LEGEND>
  162.  
  163. <p>
  164. <h:messages />
  165. </p>
  166. <p>
  167. <h:outputLabel class="field" for="username" value="#{msg['login.username']}" />
  168. <h:inputText class="textboxright" id="username" value="#{login.username}" />
  169. </p>
  170. <p>
  171. <h:outputLabel class="field" for="password" value="#{msg['login.password']}" />
  172. <h:inputSecret class="textboxright" id="password" value="#{login.password}" />
  173. </p>
  174. <p>
  175. <h:commandButton class="login" value="#{msg['login.loginAction']}" action="#{login.loginAction}" />
  176.  
  177. <h:commandLink class="left" value="#{msg['login.registerAction']}" action="#{login.registerAction}" />
  178. </p>
  179.  
  180. </fieldset>
  181. </h:form>
  182. </div>
  183. </h:body>
  184. </html>
  185.  
  186. <?xml version="1.0" encoding="UTF-8"?>
  187. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  188. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  189. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  190. id="norducparts-frontend" version="2.5">
  191. <display-name>NordicpartsFrontend</display-name>
  192.  
  193. <servlet>
  194. <servlet-name>Faces Servlet</servlet-name>
  195. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  196. <load-on-startup>1</load-on-startup>
  197. </servlet>
  198. <servlet-mapping>
  199. <servlet-name>Faces Servlet</servlet-name>
  200. <url-pattern>*.jsf</url-pattern>
  201. </servlet-mapping>
  202. <servlet-mapping>
  203. <servlet-name>Faces Servlet</servlet-name>
  204. <url-pattern>*.xhtml</url-pattern>
  205. </servlet-mapping>
  206. <servlet-mapping>
  207. <servlet-name>Faces Servlet</servlet-name>
  208. <url-pattern>/faces/*</url-pattern>
  209. </servlet-mapping>
  210. <servlet-mapping>
  211. <servlet-name>Faces Servlet</servlet-name>
  212. <url-pattern>/views/*</url-pattern>
  213. </servlet-mapping>
  214.  
  215. <filter>
  216. <filter-name>LoginFilter</filter-name>
  217. <filter-class>ro.nordicparts.odin.frontend.filters.AuthenticationFilter</filter-class>
  218. </filter>
  219. <filter-mapping>
  220. <filter-name>LoginFilter</filter-name>
  221. <servlet-name>Faces Servlet</servlet-name>
  222. <url-pattern>/views/*</url-pattern>
  223. </filter-mapping>
  224.  
  225. <context-param>
  226. <param-name>org.richfaces.skin</param-name>
  227. <param-value>DEFAULT</param-value>
  228. </context-param>
  229. <context-param>
  230. <param-name>org.richfaces.fileUpload.maxRequestSize</param-name>
  231. <param-value>100000</param-value>
  232. </context-param>
  233. <context-param>
  234. <param-name>org.richfaces.fileUpload.createTempFiles</param-name>
  235. <param-value>false</param-value>
  236. </context-param>
  237. <context-param>
  238. <param-name>javax.faces.PROJECT_STAGE</param-name>
  239. <param-value>Development</param-value>
  240. </context-param>
  241. <context-param>
  242. <param-name>org.richfaces.resourceOptimization.enabled</param-name>
  243. <param-value>true</param-value>
  244. </context-param>
  245. <context-param>
  246. <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  247. <param-value>server</param-value>
  248. </context-param>
  249.  
  250. <context-param>
  251. <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
  252. <param-value>true</param-value>
  253. </context-param>
  254. <context-param>
  255. <param-name>javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER</param-name>
  256. <param-value>true</param-value>
  257. </context-param>
  258.  
  259. <context-param>
  260. <param-name>contextConfigLocation</param-name>
  261. <param-value>classpath:ro/nordicparts/odin/frontend/application-config.xml</param-value>
  262. </context-param>
  263.  
  264. <context-param>
  265. <param-name>log4jConfigLocation</param-name>
  266. <param-value>classpath:ro/nordicparts/odin/config/log4j.xml</param-value>
  267. </context-param>
  268.  
  269. <welcome-file-list>
  270. <welcome-file>views/index.xhtml</welcome-file>
  271. </welcome-file-list>
  272.  
  273. <error-page>
  274. <exception-type>javax.faces.application.ViewExpiredException</exception-type>
  275. <location>/error.xhtml</location>
  276. </error-page>
  277.  
  278. <listener>
  279. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  280. </listener>
  281.  
  282. <listener>
  283. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  284. </listener>
  285.  
  286. <listener>
  287. <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  288. </listener>
  289. </web-app>
  290.  
  291. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  292. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  293. <parent>
  294. <artifactId>odin</artifactId>
  295. <groupId>ro.nordicparts</groupId>
  296. <version>1.0-SNAPSHOT</version>
  297. </parent>
  298. <modelVersion>4.0.0</modelVersion>
  299.  
  300. <groupId>ro.nordicparts</groupId>
  301. <artifactId>odin.frontend</artifactId>
  302. <packaging>war</packaging>
  303. <version>1.1</version>
  304.  
  305. <name>odin.frontend</name>
  306.  
  307. <properties>
  308. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  309. <richfaces.version>4.3.5.FINAL</richfaces.version>
  310. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  311. <spring.version>4.0.0.RELEASE</spring.version>
  312. <hibernate.version>4.3.0.Final</hibernate.version>
  313. <hibernate.tools.version>3.2.0.beta8</hibernate.tools.version>
  314. <hibernate.generic.dao>1.2.0</hibernate.generic.dao>
  315. <slf4j.version>1.6.4</slf4j.version>
  316. <junit.version>4.7</junit.version>
  317. <log4j.version>1.2.14</log4j.version>
  318. <commons-dbcp.version>1.4</commons-dbcp.version>
  319. <jersey.version>1.1.5</jersey.version>
  320. <servlet-api-version>2.5</servlet-api-version>
  321. <modules.version>1.0-SNAPSHOT</modules.version>
  322.  
  323. </properties>
  324.  
  325. <dependencyManagement>
  326. <dependencies>
  327. <dependency>
  328. <groupId>org.richfaces</groupId>
  329. <artifactId>richfaces-bom</artifactId>
  330. <version>${richfaces.version}</version>
  331. <scope>import</scope>
  332. <type>pom</type>
  333. </dependency>
  334. </dependencies>
  335. </dependencyManagement>
  336.  
  337. <dependencies>
  338.  
  339. <dependency>
  340. <groupId>${project.groupId}</groupId>
  341. <artifactId>odin.model</artifactId>
  342. <version>${modules.version}</version>
  343. </dependency>
  344. <dependency>
  345. <groupId>${project.groupId}</groupId>
  346. <artifactId>odin.business</artifactId>
  347. <version>${modules.version}</version>
  348. </dependency>
  349. <dependency>
  350. <groupId>${project.groupId}</groupId>
  351. <artifactId>odin.persistence</artifactId>
  352. <version>${modules.version}</version>
  353. </dependency>
  354. <dependency>
  355. <groupId>${project.groupId}</groupId>
  356. <artifactId>odin.config</artifactId>
  357. <version>${modules.version}</version>
  358. </dependency>
  359.  
  360. <dependency>
  361. <groupId>org.richfaces.core</groupId>
  362. <artifactId>richfaces-core-impl</artifactId>
  363. </dependency>
  364. <dependency>
  365. <groupId>org.richfaces.ui</groupId>
  366. <artifactId>richfaces-components-api</artifactId>
  367. </dependency>
  368. <dependency>
  369. <groupId>org.richfaces.ui</groupId>
  370. <artifactId>richfaces-components-ui</artifactId>
  371. </dependency>
  372.  
  373. <dependency>
  374. <groupId>org.apache.myfaces.core</groupId>
  375. <artifactId>myfaces-api</artifactId>
  376. </dependency>
  377. <dependency>
  378. <groupId>org.apache.myfaces.core</groupId>
  379. <artifactId>myfaces-impl</artifactId>
  380. </dependency>
  381.  
  382. <dependency>
  383. <groupId>javax.servlet</groupId>
  384. <artifactId>javax.servlet-api</artifactId>
  385. <scope>provided</scope>
  386. </dependency>
  387. <dependency>
  388. <groupId>javax.servlet.jsp</groupId>
  389. <artifactId>jsp-api</artifactId>
  390. <scope>provided</scope>
  391. </dependency>
  392.  
  393. <dependency>
  394. <groupId>javax.servlet.jsp.jstl</groupId>
  395. <artifactId>jstl-api</artifactId>
  396. <exclusions>
  397. <exclusion>
  398. <artifactId>servlet-api</artifactId>
  399. <groupId>javax.servlet</groupId>
  400. </exclusion>
  401. </exclusions>
  402. </dependency>
  403.  
  404. <dependency>
  405. <groupId>org.springframework</groupId>
  406. <artifactId>spring-web</artifactId>
  407. <version>${spring.version}</version>
  408. </dependency>
  409. <dependency>
  410. <groupId>org.springframework</groupId>
  411. <artifactId>spring-context</artifactId>
  412. <version>${spring.version}</version>
  413. </dependency>
  414. <dependency>
  415. <groupId>org.springframework</groupId>
  416. <artifactId>spring-orm</artifactId>
  417. <version>${spring.version}</version>
  418. </dependency>
  419. <dependency>
  420. <groupId>org.springframework</groupId>
  421. <artifactId>spring-core</artifactId>
  422. <version>${spring.version}</version>
  423. </dependency>
  424.  
  425. <dependency>
  426. <groupId>net.sourceforge.cssparser</groupId>
  427. <artifactId>cssparser</artifactId>
  428. <version>0.9.7</version>
  429. </dependency>
  430. <dependency>
  431. <groupId>com.google.guava</groupId>
  432. <artifactId>guava</artifactId>
  433. <version>14.0.1</version>
  434. </dependency>
  435. <dependency>
  436. <groupId>org.w3c.css</groupId>
  437. <artifactId>sac</artifactId>
  438. <version>1.3</version>
  439. </dependency>
  440. <dependency>
  441. <groupId>antlr</groupId>
  442. <artifactId>antlr</artifactId>
  443. <version>2.7.6rc1</version>
  444. </dependency>
  445. <dependency>
  446. <groupId>asm</groupId>
  447. <artifactId>asm</artifactId>
  448. <version>1.5.3</version>
  449. </dependency>
  450. <dependency>
  451. <groupId>asm</groupId>
  452. <artifactId>asm-attrs</artifactId>
  453. <version>1.5.3</version>
  454. </dependency>
  455. <dependency>
  456. <groupId>c3p0</groupId>
  457. <artifactId>c3p0</artifactId>
  458. <version>0.9.0</version>
  459. </dependency>
  460. <dependency>
  461. <groupId>cglib</groupId>
  462. <artifactId>cglib</artifactId>
  463. <version>3.0</version>
  464. </dependency>
  465.  
  466. <dependency>
  467. <groupId>concurrent</groupId>
  468. <artifactId>concurrent</artifactId>
  469. <version>1.3.2</version>
  470. </dependency>
  471. <dependency>
  472. <groupId>dom4j</groupId>
  473. <artifactId>dom4j</artifactId>
  474. <version>1.6.1</version>
  475. </dependency>
  476. <dependency>
  477. <groupId>javax.security</groupId>
  478. <artifactId>jaas</artifactId>
  479. <version>1.0.01</version>
  480. </dependency>
  481. <dependency>
  482. <groupId>javax.security</groupId>
  483. <artifactId>jacc</artifactId>
  484. <version>1.0</version>
  485. </dependency>
  486. <dependency>
  487. <groupId>jaxen</groupId>
  488. <artifactId>jaxen</artifactId>
  489. <version>1.1</version>
  490. </dependency>
  491. <dependency>
  492. <groupId>javax.transaction</groupId>
  493. <artifactId>jta</artifactId>
  494. <version>1.1</version>
  495. </dependency>
  496. <dependency>
  497. <groupId>oscache</groupId>
  498. <artifactId>oscache</artifactId>
  499. <version>2.2</version>
  500. </dependency>
  501. <dependency>
  502. <groupId>proxool</groupId>
  503. <artifactId>proxool</artifactId>
  504. <version>0.8.3</version>
  505. </dependency>
  506.  
  507. <dependency>
  508. <groupId>org.w3c.css</groupId>
  509. <artifactId>sac</artifactId>
  510. <version>1.3</version>
  511. </dependency>
  512. <dependency>
  513. <groupId>org.slf4j</groupId>
  514. <artifactId>slf4j-ext</artifactId>
  515. <version>1.7.5</version>
  516. </dependency>
  517.  
  518. <dependency>
  519. <groupId>org.slf4j</groupId>
  520. <artifactId>slf4j-api</artifactId>
  521. <version>${slf4j.version}</version>
  522. </dependency>
  523. <dependency>
  524. <groupId>org.slf4j</groupId>
  525. <artifactId>slf4j-log4j12</artifactId>
  526. <version>${slf4j.version}</version>
  527. </dependency>
  528.  
  529. <dependency>
  530. <groupId>swarmcache</groupId>
  531. <artifactId>swarmcache</artifactId>
  532. <version>1.0RC2</version>
  533. </dependency>
  534. <dependency>
  535. <groupId>xalan</groupId>
  536. <artifactId>xalan</artifactId>
  537. <version>2.7.0</version>
  538. </dependency>
  539. <dependency>
  540. <groupId>xerces</groupId>
  541. <artifactId>xmlParserAPIs</artifactId>
  542. <version>2.6.2</version>
  543. </dependency>
  544. <dependency>
  545. <groupId>xerces</groupId>
  546. <artifactId>xerces</artifactId>
  547. <version>2.4.0</version>
  548. </dependency>
  549. <dependency>
  550. <groupId>xml-apis</groupId>
  551. <artifactId>xml-apis</artifactId>
  552. <version>2.0.2</version>
  553. </dependency>
  554. </dependencies>
  555.  
  556. <build>
  557. <finalName>NordicpartsFrontend</finalName>
  558. <plugins>
  559. <plugin>
  560. <artifactId>maven-war-plugin</artifactId>
  561. <configuration>
  562. <archive>
  563. <manifest>
  564. <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
  565. </manifest>
  566. </archive>
  567. </configuration>
  568. </plugin>
  569. </plugins>
  570. </build>
  571. </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement